Derek Hart wrote:
Quote:
How can I set the listbox value and retrieve a listbox value with just text?
I have filled the listbox with items in the items collection property (very
simple, just one column of text items).
>
I will have the text from a database and I know it is in the list. How can I
set it and retrieve it?
>
Do I need to loop the listbox to find matching text and set that
selectedindex to true.
>
Any sample code would be appreciated to get the text and set the text.
>
>
ListBox has FindString and FindStringExact methods.
Here's a sample to get started:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
ListBox1.Items.Add("Banana")
ListBox1.Items.Add("is a")
ListBox1.Items.Add("fruit")
ListBox1.Items.Add("but")
ListBox1.Items.Add("monkey")
ListBox1.Items.Add("is an")
ListBox1.Items.Add("animal")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ThisIndex As Integer
ThisIndex = ListBox1.FindStringExact("monkey")
If ThisIndex >= 0 Then
MessageBox.Show(ListBox1.Items(ThisIndex).ToString , _
"ListBox", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
ListBox1.SelectedIndex = ThisIndex
ListBox1.Items(ThisIndex) = "donkey"
End If
End Sub
--
Teme64 @
http://windevblog.blogspot.com