ListBox.FindString 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
查找以指定字符串开头的第一项 ListBox 。
重载
| 名称 | 说明 |
|---|---|
| FindString(String) |
查找以指定字符串开头的第一项 ListBox 。 |
| FindString(String, Int32) |
查找以指定字符串开头的第一项 ListBox 。 搜索从特定的起始索引开始。 |
FindString(String)
- Source:
- ListBox.cs
- Source:
- ListBox.cs
- Source:
- ListBox.cs
- Source:
- ListBox.cs
- Source:
- ListBox.cs
查找以指定字符串开头的第一项 ListBox 。
public:
int FindString(System::String ^ s);
public int FindString(string s);
member this.FindString : string -> int
Public Function FindString (s As String) As Integer
参数
- s
- String
要搜索的文本。
返回
找到的第一项的从零开始的索引;如果未找到匹配项,则 ListBox.NoMatches 返回。
例外
参数的值 s 小于 -1 或大于或等于项计数。
示例
下面的代码示例演示如何使用 FindString 该方法搜索字符串的第一个 ListBox实例。 如果未找到与搜索字符串 FindString 匹配的项目将返回 -1 值,并且该示例显示一个 MessageBox。 如果找到与搜索文本匹配的项,该示例将使用 SetSelected 该方法选择搜索文本中的 ListBox项。
private:
void FindMyString( String^ searchString )
{
// Ensure we have a proper string to search for.
if ( searchString != String::Empty )
{
// Find the item in the list and store the index to the item.
int index = listBox1->FindString( searchString );
// Determine if a valid index is returned. Select the item if it is valid.
if ( index != -1 )
listBox1->SetSelected( index, true );
else
MessageBox::Show( "The search string did not match any items in the ListBox" );
}
}
private void FindMyString(string searchString)
{
// Ensure we have a proper string to search for.
if (!string.IsNullOrEmpty(searchString))
{
// Find the item in the list and store the index to the item.
int index = listBox1.FindString(searchString);
// Determine if a valid index is returned. Select the item if it is valid.
if (index != -1)
listBox1.SetSelected(index,true);
else
MessageBox.Show("The search string did not match any items in the ListBox");
}
}
Private Sub FindMyString(ByVal searchString As String)
' Ensure we have a proper string to search for.
If searchString <> String.Empty Then
' Find the item in the list and store the index to the item.
Dim index As Integer = listBox1.FindString(searchString)
' Determine if a valid index is returned. Select the item if it is valid.
If index <> -1 Then
listBox1.SetSelected(index, True)
Else
MessageBox.Show("The search string did not match any items in the ListBox")
End If
End If
End Sub
注解
此方法执行的搜索不区分大小写。 搜索查找部分匹配指定搜索字符串参数 s的单词。 可以使用此方法搜索与指定字符串匹配的第一项。 然后,可以使用该方法或更改项的文本来执行任务,例如删除包含搜索文本 Remove 的项。 找到指定文本后,如果要搜索其中 ListBox其他文本实例,则可以使用提供参数的方法版本 FindString 来指定其中的 ListBox起始索引。 如果要对确切的字词匹配而不是部分匹配执行搜索,请使用该方法 FindStringExact 。
另请参阅
适用于
FindString(String, Int32)
- Source:
- ListBox.cs
- Source:
- ListBox.cs
- Source:
- ListBox.cs
- Source:
- ListBox.cs
- Source:
- ListBox.cs
查找以指定字符串开头的第一项 ListBox 。 搜索从特定的起始索引开始。
public:
int FindString(System::String ^ s, int startIndex);
public int FindString(string s, int startIndex);
member this.FindString : string * int -> int
Public Function FindString (s As String, startIndex As Integer) As Integer
参数
- s
- String
要搜索的文本。
- startIndex
- Int32
要搜索的第一项之前项的从零开始的索引。 设置为负一(-1),从控件的开头进行搜索。
返回
找到的第一项的从零开始的索引;如果未找到匹配项,则 ListBox.NoMatches 返回。
例外
参数startIndex小于零或大于或等于类的属性Count的值ListBox.ObjectCollection。
示例
下面的代码示例演示如何使用 FindString 该方法在项 ListBox中搜索搜索文本的所有实例。 该示例使用方法的版本 FindString ,可用于指定起始搜索索引,以便从中连续搜索所有项 ListBox。 该示例还演示如何在 FindString 方法到达项列表底部后从列表顶部开始搜索,以防止递归搜索。 在找到项后 ListBox,将使用该方法 SetSelected 选择它们。
private:
void FindAllOfMyString( String^ searchString )
{
// Set the SelectionMode property of the ListBox to select multiple items.
listBox1->SelectionMode = SelectionMode::MultiExtended;
// Set our intial index variable to -1.
int x = -1;
// If the search string is empty exit.
if ( searchString->Length != 0 )
{
// Loop through and find each item that matches the search string.
do
{
// Retrieve the item based on the previous index found. Starts with -1 which searches start.
x = listBox1->FindString( searchString, x );
// If no item is found that matches exit.
if ( x != -1 )
{
// Since the FindString loops infinitely, determine if we found first item again and exit.
if ( listBox1->SelectedIndices->Count > 0 )
{
if ( x == listBox1->SelectedIndices[ 0 ] )
return;
}
// Select the item in the ListBox once it is found.
listBox1->SetSelected( x, true );
}
}
while ( x != -1 );
}
}
private void FindAllOfMyString(string searchString)
{
// Set the SelectionMode property of the ListBox to select multiple items.
listBox1.SelectionMode = SelectionMode.MultiExtended;
// Set our intial index variable to -1.
int x =-1;
// If the search string is empty exit.
if (searchString.Length != 0)
{
// Loop through and find each item that matches the search string.
do
{
// Retrieve the item based on the previous index found. Starts with -1 which searches start.
x = listBox1.FindString(searchString, x);
// If no item is found that matches exit.
if (x != -1)
{
// Since the FindString loops infinitely, determine if we found first item again and exit.
if (listBox1.SelectedIndices.Count > 0)
{
if(x == listBox1.SelectedIndices[0])
return;
}
// Select the item in the ListBox once it is found.
listBox1.SetSelected(x,true);
}
}while(x != -1);
}
}
Private Sub FindAllOfMyString(ByVal searchString As String)
' Set the SelectionMode property of the ListBox to select multiple items.
listBox1.SelectionMode = SelectionMode.MultiExtended
' Set our intial index variable to -1.
Dim x As Integer = -1
' If the search string is empty exit.
If searchString.Length <> 0 Then
' Loop through and find each item that matches the search string.
Do
' Retrieve the item based on the previous index found. Starts with -1 which searches start.
x = listBox1.FindString(searchString, x)
' If no item is found that matches exit.
If x <> -1 Then
' Since the FindString loops infinitely, determine if we found first item again and exit.
If ListBox1.SelectedIndices.Count > 0 Then
If x = ListBox1.SelectedIndices(0) Then
Return
End If
End If
' Select the item in the ListBox once it is found.
ListBox1.SetSelected(x, True)
End If
Loop While x <> -1
End If
End Sub
注解
此方法执行的搜索不区分大小写。 搜索查找部分匹配指定搜索字符串参数 s的单词。 可以使用此方法在项列表中 ListBox搜索与指定起始索引处的指定字符串匹配的第一项。 然后,可以使用该方法或更改项的文本来执行任务,例如删除包含搜索文本 Remove 的项。 此方法通常在使用未指定起始索引的此方法版本进行调用后使用。 在列表中找到初始项后,此方法通常用于通过在搜索文本的第一个找到实例之后在项的参数中 startIndex 指定索引位置来查找搜索文本的更多实例。 如果要对确切的字词匹配而不是部分匹配执行搜索,请使用该方法 FindStringExact 。