problem setting more than one selected value in a list box

G

Guest

Hi I have a listbox and have the selection mode set to multiple in the
property box.
I am trying to set multiple selected values in code but it only seems to use
the last one.
listbox.SelectedValue = 1
listbox.SelectedValue = 2
thanks.
only 2 gets set above.
 
S

Simon Harris

ListBox.Items(0).Value = "Foo"
ListBox.Items(1).Value = "Foo1"

The number (0) is the ordinal reference of the list box item, zero based,
starting from the top.

Your code was actually setting the selected value, as would happen if the
user clicked the item on the list.

Hope that helps!

Simon.
 
Y

Yunus Emre ALPÖZEN [MCAD.NET]

allow multiple selection and set items as selected not selected value

listbox.Items[0].Selected=true; etc...
 
S

Simon Harris

Oops - Forget my answer, totally mis-read your post! Zzzz....

Simon Harris said:
ListBox.Items(0).Value = "Foo"
ListBox.Items(1).Value = "Foo1"

The number (0) is the ordinal reference of the list box item, zero based,
starting from the top.

Your code was actually setting the selected value, as would happen if the
user clicked the item on the list.

Hope that helps!

Simon.
 
S

Shawn

Hi,
you can do something like this:

Dim item As ListItem

For Each item in ListBox1.Items
If item.Text = "something" Then
item.Selected = True
End If
Next

Shawn
 
G

Guest

got it to work, code below seemed to work ok.

listbox.Items.FindByValue("1").Selected = True
listbox.Items.FindByValue("2").Selected = True
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top