Visual Basic 中学校 > Visual Basic サンプル集 > Visual Basic サンプル集目次 > Windows.Forms.ListBox >
項目を検索する
1.「宮」から始まる項目の位置を調べる例
Dim Index As Integer Index = ListBox1.FindString("宮")
If Index = ListBox.NoMatches Then
MsgBox("項目は存在しません。")
Else
MsgBox(Index + 1 & "番目にあります。")
End Ifこの例では、先頭から検索し、「宮城」や「宮崎」などはじめに「宮」が付く項目の位置を表示する。
2.6番目以降の項目で「宮」から始まる項目の位置を調べる例
Dim Index As Integer Index = ListBox1.FindString("宮", 5)
If Index = ListBox.NoMatches Then
MsgBox("項目は存在しません。")
Else
MsgBox(Index + 1 & "番目にあります。")
End Ifこの例では、6番目から検索し、「宮城」や「宮崎」などはじめに「宮」が付く項目の位置を表示する。
3.「宮」という項目の位置を調べる例
Dim Index As Integer Index = ListBox1.FindStringExact("宮")
If Index = ListBox.NoMatches Then
MsgBox("項目は存在しません。")
Else
MsgBox(Index + 1 & "番目にあります。")
End Ifこの例では、先頭から検索し、「宮」という項目の位置を表示する。「宮城」や「宮崎」にはヒットしない。