IBindingList.Find(PropertyDescriptor, Object) 方法

定義

傳回具有指定 PropertyDescriptor 的列索引。

public:
 int Find(System::ComponentModel::PropertyDescriptor ^ property, System::Object ^ key);
public int Find (System.ComponentModel.PropertyDescriptor property, object key);
abstract member Find : System.ComponentModel.PropertyDescriptor * obj -> int
Public Function Find (property As PropertyDescriptor, key As Object) As Integer

參數

property
PropertyDescriptor

要在其上搜尋的 PropertyDescriptor

key
Object

要搜尋的 property 參數值。

傳回

具有指定 PropertyDescriptor 的列索引。

例外狀況

範例

下列程式代碼範例示範如何實作 Find 方法。

    public class MyFontList : BindingList<Font>
    {

        protected override bool SupportsSearchingCore
        {
            get { return true; }
        }
        protected override int FindCore(PropertyDescriptor prop, object key)
        {
            // Ignore the prop value and search by family name.
            for (int i = 0; i < Count; ++i)
            {
                if (Items[i].FontFamily.Name.ToLower() == ((string)key).ToLower())
                    return i;
            }
            return -1;
        }
    }
}
Public Class MyFontList
    Inherits BindingList(Of Font)

    Protected Overrides ReadOnly Property SupportsSearchingCore() As Boolean
        Get
            Return True
        End Get
    End Property
    
    Protected Overrides Function FindCore(ByVal prop As PropertyDescriptor, _
        ByVal key As Object) As Integer
        ' Ignore the prop value and search by family name.
        Dim i As Integer
        While i < Count
            If Items(i).FontFamily.Name.ToLower() = CStr(key).ToLower() Then
                Return i
            End If
            i += 1
        End While

        Return -1
    End Function
End Class

備註

這個方法會選取第一個數據列,其中 參數的值 property 等於 參數的值 key

如果 是 SupportsSearchingtrue,則支援這個方法,否則這個方法會擲回 NotSupportedException

適用於