IBindingList.Find(PropertyDescriptor, Object) 方法

定義

傳回具有指定 PropertyDescriptor 的列索引。Returns the index of the row that has the given 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

要在其上搜尋的 PropertyDescriptorThe PropertyDescriptor to search on.

key
Object

要搜尋的 property 參數值。The value of the property parameter to search for.

傳回

具有指定 PropertyDescriptor 的列索引。The index of the row that has the given PropertyDescriptor.

例外狀況

範例

下列程式碼範例示範如何執行Find方法。The following code example demonstrates how to implement the Find method.

    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參數的值。This method will select the first row where the value of the property parameter equals the value of the key parameter.

如果SupportsSearching NotSupportedExceptiontrue, 則支援這個方法, 否則這個方法會擲回。This method is supported if SupportsSearching is true, otherwise this method throws a NotSupportedException.

適用於