Set Listbox Value

  • Thread starter Thread starter Derek Hart
  • Start date Start date
D

Derek Hart

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.
 
Derek said:
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
 
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 a SelectedValue property. I don't know how it behaves if
the Listbox allows multiple items to be selected.
 
Derek,

In the same way as a Combobox

Create a collection by instance a datatable which is the most simple to use
with a listbox

Set that as the DataSource
Set one column to the displaymember
Set another column to the valuemember

You are ready

Cor
 
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.

Did

myListBox.Text = someTextIGotFromMyDatabase

not work for you?
 
Back
Top