ToolstripComboBox

Moorzee

Well-known member
Joined
May 31, 2006
Messages
92
Location
England
Programming Experience
3-5
Has anyone ever used this as a normal combobox i.e with a diplsymember and valuemember?

I have been killing myself with this for a while now!

There is an underlying ComboBox which I have set datasource,displaymember, and valuemember but cannot use.......

Please someone put me out of my misery. Toes are curling up in frustration and grinding my teeth!!!:(

Ta.
 

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,015
Location
Cambridge, UK
Programming Experience
5-10
As far as i know the toolstripcombobox is just a host for a normal combobox but you have to cast the the hosted control.

Code:
Dim cb As ComboBox = DirectCast(toolStripComboBox1.Control, ComboBox)
cb.DataSource = someDataTable
cb.DisplayMember = "columname-for-text"
cb.ValueMember = "colunmname-for-values"
 

Moorzee

Well-known member
Joined
May 31, 2006
Messages
92
Location
England
Programming Experience
3-5
Right over my head that. Never seen directcast before.

Tried your suggestion vis,cheers,didn't fall over but nothing displayed in the toolstripcombobox on screen. Not my day:(
 

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,015
Location
Cambridge, UK
Programming Experience
5-10
Really, i'm in shock! There's absolutley no reason why that shouldn't work. I'll look into it a bit more. It's just a combobox like any other. It inherits from the combobox class. I'd have to have a look and see what is going on under the hood to stop it from behaving correctly.
 

Moorzee

Well-known member
Joined
May 31, 2006
Messages
92
Location
England
Programming Experience
3-5
Right, 'tis a bug methinks. I can populate a toolstripcombo fine with your suggestion earlier(thanks vis) but only if the combo is at the top level on the toolstrip. This aint what I require. I'm looking to build a menu structure for searches so menu would be something along the lines of:
Clients:
--->Client Searches:
------>Search By Status(a tsCombo of statuses I have on DB)*PROB*
------>Search By Surname(A textbox to bung in a like%% param)
etc..

:confused: :( :( Gutted:( :( :confused:
 

Moorzee

Well-known member
Joined
May 31, 2006
Messages
92
Location
England
Programming Experience
3-5
I can't see why not. My menu would be logical. The parameter I require is a status(smallint) I populate a toolstripcombo with my status descriptions and valuemember of the code and as soon as the user selects a status from the toolstrip combo stored proc is fired and results returned.

Am I being mental?? :)
 

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,015
Location
Cambridge, UK
Programming Experience
5-10
No, that sounds good to me. It's really that toolstrip combo that bugging me. I tried it last night and i couldn't get it to work. It doesn't make sense seeing as it's a normal combobox behind the scenes. But i did find a different solution on the web. It's not very elegant but it sounds like it could work....

Basically this guy had loaded the combobox manually in the forms constructor and created a private arraylist to hold the value member. Used the combobox to do a lookup of the valuemember in the arraylist and passed that to the tableadapter. It sounds a bit much, but apart from that there doesn't seem to be much else that you can do.
Bit of a pain, eh?
 

Moorzee

Well-known member
Joined
May 31, 2006
Messages
92
Location
England
Programming Experience
3-5
Basically this guy had loaded the combobox manually in the forms constructor and created a private arraylist to hold the value member. Used the combobox to do a lookup of the valuemember in the arraylist and passed that to the tableadapter. It sounds a bit much, but apart from that there doesn't seem to be much else that you can do.
Bit of a pain, eh?
I was thinking of something similar, but perhaps a bit sloppier, another combo populated at same time from a datareader, and made not visible then every selectedindexchanged event get the correspondoding value from the invisible combo.... Really should'nt have to though as you sai it is simply a combo whatever level of the structure you have it!!!:(
 

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,015
Location
Cambridge, UK
Programming Experience
5-10
Just having another think. We know it works fine at the top level but not elsewhere. There really is no reason for this. Could you share you code for what your are trying to do?
 

Moorzee

Well-known member
Joined
May 31, 2006
Messages
92
Location
England
Programming Experience
3-5
I have got all the datasets and commands etc.. set up for my back end but you easily replicate by creating a top level combo populate with the cast and binding you put me on to then cut the control and embed further down a structure. Add a Dropdownbutton then a menuitem under that and place your combo across from this item!

No show?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,283
Location
Sydney, Australia
Programming Experience
10+
I tried the same thing and can confirm that the items are not displayed. I checked the DataSource property of the control at various points and it was set to a table but no items were displayed. I have no idea why this would be the case but I'd agree that it must be a bug. You should report it to Microsoft but in the meantime you could get around it by just adding the items manually and using a keyed collection to setup the correspondence between the two properties if required.
 

Moorzee

Well-known member
Joined
May 31, 2006
Messages
92
Location
England
Programming Experience
3-5
I've reported the behaviour as a bug.
I've also implemented the idea of an array of int's to store my values and gained them from selectedindex of tstripcombo.
Whilst I'm here does anybody have any idea why when I select an option in my tstripcbo and code behind the SelectedIndexChanged event the menu structure stays visible, i.e still see the option under top level drpdownbtn, then the menuitem chosen then the actual cbo?

Is there a method I am missing a call to at the end of selection process to redraw the tstrip back to toplevel?

Cheers.
 

Moorzee

Well-known member
Joined
May 31, 2006
Messages
92
Location
England
Programming Experience
3-5
yeah, I click which ever row in my toolstripcombo and the selectedindexchanged event fires, my code is run(stored proc fired, results displayed in grid) but path taken to combo is still displayed from the toolstrip... Not particularly good english that I know.:eek:

Do you get what I am saying?

I have found that if I place a call to the toolstripcombo.PerformClick() event after my code has executed then the toolstrip is rendered as I would expect, no 2nd/3rd level items are displayed.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,409
Location
Norway
Programming Experience
10+
About combo binding, ToolStripComboBox class has got a ComboBox property that expose the regular ComboBox control it hosts (doc: "to get properties, methods, and events that have not been wrapped"), which should allow you to treat it as such also.

Another option is to use the ToolStripControlHost to add a regular ComboBox control, which you bind.
 

netnewb

Member
Joined
Feb 4, 2008
Messages
9
Programming Experience
10+
YES! That works!

JohnH, thanks! That worked.

Just use toolstripcombobox1.combobox.datasource, .valuemember, and .displaymember

I search about 6 google hits before I found this.
 

timh

Well-known member
Joined
Nov 28, 2008
Messages
50
Programming Experience
Beginner
I tried the same thing and can confirm that the items are not displayed. I checked the DataSource property of the control at various points and it was set to a table but no items were displayed. I have no idea why this would be the case but I'd agree that it must be a bug. You should report it to Microsoft but in the meantime you could get around it by just adding the items manually and using a keyed collection to setup the correspondence between the two properties if required.
Sorry to "bump" such an old post, but I am curious to know whether this "bug" has ever been addressed? I'm having the exact same issue in VS Express for Desktop.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,409
Location
Norway
Programming Experience
10+
The BindingContext property of the hosted ComboBox is not initialized when it is added as subitem in toolstrip, set it to for example same object as the forms BindingContext property - that is default for comboboxes you add directly to a form and for strip top-level ToolStripComboBoxes.
 
Top Bottom