Loading ListBox

bert beat

New member
Joined
Mar 3, 2005
Messages
1
Programming Experience
Beginner
Hey guys

i got e question for you

example of my question

"Begin Program Code

dim MainArray() as string {"Array1","Array2"}
dim Array1() as string {"Array1Tes1","Array1Test2"}
dim Array2() as string {"Array2Tes1","Array2Test2"}

cboArrayloading.DataSource = MainArray

lstBox.DataSource = cboArrayLoading.SelectedItem

"End Program Code

"How come that i get an error, i just want to load the data from Array1 or Array2 (depending on which array i selected in my combobox) in the listbox.

I'm sure one of you can give me an answer to my suestion":cool:
 

tresqboy

Member
Joined
Sep 6, 2005
Messages
7
Location
Manchester NH/Boston MA
Programming Experience
3-5
This is your problem
lstBox.DataSource = cboArrayloading.SelectedItem

In your case, you have been passing a pure string value "Array1 or Array2" and trying to do databinding on that string. You can do a databind if you a passing an object that impliments IList ( string doesn't).
There are few ways that you can establish databinding there. One of the ways to use if else then/select...case
1 declare mainArray, Array1, Array2 as global arrays
2 do databinding for a combobox (probably on the form Load event or whenever)
3 perform a listbox databinding on the eventhandler for a combobox SelectedIndexChanged event...below is the example for that eventhandler
VB.NET:
[size=2][color=#0000ff]Private Sub[/color] cboArrayloading_SelectedIndexChanged([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] cboArrayloading.SelectedIndexChanged[indent][color=#0000ff]If [/color][color=#0000ff]Me[/color].cboArrayloading.SelectedItem = "Array2" [color=#0000ff]Then 
[/color][color=#0000ff]   Me[/color].lstBox.DataSource = [color=#0000ff]Me[/color].Array2
[color=#0000ff]ElseIf [/color][color=#0000ff]Me[/color].cboArrayloading.SelectedItem = "Array1" [color=#0000ff]Then
 [/color][color=#0000ff] Me[/color].lstBox.DataSource = [color=#0000ff]Me[/color].Array1
[color=#0000ff]Else
 Me[/color].lstBox.DataSource = [color=#0000ff]Nothing
[/color][color=#0000ff]End [/color][color=#0000ff]If[/color][/indent]
[color=#0000ff]End Sub[/color][/size]
PS: That guideline assumes that arraynames are known at the design time and there. but what if the data is not known and the source may change? Then you may try to use a better means of collections than array. You may use hashtable, sortedlist, xml document or a datatable in place or arrays
 
Last edited by a moderator:

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
I have a slightly better idea that I would use.

Notice I declare another array MainArray1 and use it to hold the arrays.
I then load the arrays into this array. Now you have one array holding a string value and the other holding the actual arrays. The reason for this is now we can exclude all the if statements and make it work in a much simpler fashion. The code should be self explanatory, if not, i will post more information. I do believe there is an even better way to do this by inposing the string "Array1" as the Variable Array1 but I have not found the answer. So far, this is the best I could do to eliminate the multiple if statements. The reason for this is if you have 100 different arrays to look for that's alot of if's and else's to write in. In the code I just sent, all you ahve to update is the declaration at the beggining of the program.


dim MainArray() as string {"Array1","Array2"}
dim Array1() as string {"Array1Tes1","Array1Test2"}
dim Array2() as string {"Array2Tes1","Array2Test2"}
Dim MainArray1() as array {Array1, Array2}


cboArrayloading.DataSource = MainArray

lstBox.DataSource = MainArray1(cboArrayLoading.SelectedIndex)

end program code....


here is the entire code....


Dim MainArray() as string {"Array1","Array2"}
Dim Array1() as string {"Array1Tes1","Array1Test2"}
Dim Array2() as string {"Array2Tes1","Array2Test2"}
Dim MainArray1() as array {Array1, Array2}

Private Sub Form1_Load(ByVal Sender as object, ByVal e as system.EventArgs) Handles MyBase.Load

combobox1.DataSource = MainArray

End Sub

Private Sub combobox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles combobox1.SelectedIndexChanged

listbox1.DataSource = MainArray1(combobox1.selectedIndex)

End Sub

"End Program Code
 
Top Bottom