how to select data to listbox dynamically in vb.net

Posted by hareeshkumarkr-16190 under VB.NET on 10/27/2013 | Points: 10 | Views : 994 | Status : [Member] | Replies : 1
Hi All,

How to select the values in Listbox dynamically ?

my code is like :

Dim temp As String = "Mysore , Bangalore , Mandya ,"
Dim temporary As String() = temp.Split(","c)
Dim length As Integer = temporary.Count()

For I As Integer = 0 To length - 1
MessageBox.Show(temporary(I))
ListBox1.Items(temporary(I)).Selected = True // i am getting error here
Next

Reagrds,
Harish




Responses

Posted by: Bandi on: 10/28/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi Harish,
You might get the following error


There are two errors in your code:
Dim temp As String = "Mysore , Bangalore , Mandya , "
1) you have placed extra comma in the string array and you have check beyond the array limit.
So I corrected it by removing comma at the end
2) ListBox1.Items(temporary(I))
Items() expects the integer value.. But you are passing array string instead of index value...

refer the following code...
        Dim temp As String = "Mysore , Bangalore , Mandya"
Dim temporary As String() = temp.Split(","c)
Dim length As Integer = temporary.Count()

For I As Integer = 0 To length - 1
'MessageBox.Show(temporary(I))
ListBox1.Items(I).Selected = True 'i am getting error here
Next


NOTE:
make sure that you have list box items added before making selection...
Before Selection of item value in listbox you must have items values in the ListBox

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

hareeshkumarkr-16190, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response