2011-11-19 38 views
0

我想在我的列表框中搜索一小段文本,如果它在列表框中,我想選擇它,然後將其轉換爲字符串。從列表框中選擇行,然後轉換爲字符串

我該怎麼做?

因爲我找不到在特定行上選擇某個東西的好命令!

感謝

+2

您能顯示您嘗試過的代碼並解釋它失敗的位置嗎? –

回答

1

要選擇列表框項,設定ListBox的SelectedIndex財產。因此,例如:

Dim stringToFind As String = "someString" 

For i As Integer = 0 To Me.MyListBox.Items.Count - 1 
    Dim itemAsString As String = Me.MyListBox.Items(i).ToString() 
    If itemAsString.Contains(stringToFind) Then 
     Me.MyLabel.Text = itemAsString 
     Me.MyListBox.SelectedIndex = i 
     Exit For 'If you're using a MultiSelect ListBox, you can add to Me.MyListBox.SelectedIndices and remove this line. 
    End If 
Next 
+0

它完美地工作,但是如何將其複製到另一個標籤? – user1054822

+0

@ user1054822:只需將'itemAsString'分配給您的標籤的'Text'屬性。我已經編輯了我的答案來包含這一點。 – Ryan

相關問題