View Full Version : .Net - how to change the DataSource for a combo box
MasterYoda
10-Nov-2004, 19:26
hi folks,
been writing a little app over the last few days, and almost got the ui done the way I want it. The only problem is that for it to work I need it to link several drop down boxes such that when one is changed they all update their options. Each is using a DataSource of type ArrayList* and I've got the lists being updated just fine - the problem is the options in the combo boxes never change from the first values set. Anyone got any ideas as to how this is done?
e.g Box 1 contains A,B,C
if Box 1 == A
Box2 contains 1,2,3
else if
Box1 == B
Box2 contains 4,5,6
else if
Box1 == C
Box2 contains 7,8,9
its a little more complicated than that ;) but thats basically the idea.
thanks,
Yoda
just to add, I've been going through the available methods for the class looking for one that sounds right but none of them seem to work :(
The Dark One
10-Nov-2004, 20:17
Not sure if this applies to .net, but don't you just set the selected value?
MasterYoda
10-Nov-2004, 20:29
I've found a bodge around it by clearing and resetting the pointer for the DataSource
I thought I had posted to say "nm" but I think Trickery is heaving under the load today
I've got a new problem now tho - I'm also changing the contents of Box 1 when Box 1 is set (basically Box 1 will only contain the item selected or a blank selection) - this changes all the indexes and its got itself into an infinite loop clearing and then setting the combo boxes. :D
oh well should be solvable.
To set it to a certain item in a combo box you can set the item by its name.
[code:1:4c342c7667]
comboBox.Text = "item's exact text name";
[/code:1:4c342c7667]
And it will choose it if it is already within the list of items selectable.
Does that help?
There is a proper way to do it using the Items collection class, there is findText() and findValue() that returns an Item object which you can then give to some method I cant remember the name of to select it as the active item.
I did a similar thing to what you wanted the otherday with asp.net :(
SylverFyre
10-Nov-2004, 23:01
are you using databinding on all the controls or are you manually setting the values according to what is in Box1 ?
MasterYoda
11-Nov-2004, 03:07
the prob wasn't setting the info, it was getting the box to display the new info after making changes but I think I have a workaround
Sylverfyre, not sure what databinding is (working this C++ and .net stuff out as I go) but I have 6 drop down boxes with 6 ArrayLists of text strings. If one of the boxes is changed all 6 ArrayLists are regenerated and the 6 drop down boxes need to refresh. The way I have gotten around it is to reassign the DataSource pointer (to the same place but nm)
You can see where the problem with the loop arises -
box 1 modfied (by recreation)? - call the handler to recreate box 1! :)
one for tomorrow me thinks
thanks all for the input :)
yoda
Ruddles
11-Nov-2004, 19:03
If you can still create control arrays in .NET then you could take the index of the box clicked and only update the indexs after it. Ie if combobox[3] is clicked then it only updates combobox[4] combobox[5] and combobox[6] by passing the index as the starting index of the loop.
If you can make sense of that.
You can make an array of a control type. . .
Ruddles
11-Nov-2004, 19:09
aye, that will do it. Sorry, last time I used it was in the days of VB6.....
vBulletin® v3.7.0 Release Candidate 3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.