|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Help creating custom ComboBox controlI'm populating a ComboBox with objects which contain two properties - IntegerValue and StringValue. I display StringValue in the ComboBox form and can retrieve IntegerValue from the SelectItem.IntegerValue property. This is all good but now I want to get a little bit more complicated. I want to be able to set the ComboBox's SelectedItem property by setting IntegerValue, so for instance if I have the following items in my ComboBox: IntegerValue StringValue 3 Apple 4 Banana 1 Carrot 5 Eggplant 2 Fig By doing something like ComboBox.SelectedItem.IntegerValue = 1 the ComboBox would then display Carrot. I imagine I would need to create a custom ComboBox control and add a function which is kicked off when the IntegerValue is Set, or perhaps just override an existing function. Has anyone done this or something similar before or have any suggestions? Regards, Mong Mong,
If you use a datatable as datasource this is in my idea dead simple. If you try it using this sample on our website http://www.vb-tips.com/default.aspx?ID=e8fee3e3-3ca7-4bb0-81dc-f0b560f43e7c And than add in this sample, 'ComboBox1.DataSource = dt 'ComboBox1.DisplayMember = "FullName" ComboBox1.DisplayValue = "LName" Combobox1.SelectedText = "Cor Ligthert" (In your case a number of course) I hope this helps. Cor <mongphon***@yahoo.com.au> schreef in bericht Show quoteHide quote news:1140396772.295608.56100@z14g2000cwz.googlegroups.com... > Hi, > > I'm populating a ComboBox with objects which contain two properties - > IntegerValue and StringValue. I display StringValue in the ComboBox > form and can retrieve IntegerValue from the SelectItem.IntegerValue > property. This is all good but now I want to get a little bit more > complicated. > > I want to be able to set the ComboBox's SelectedItem property by > setting IntegerValue, so for instance if I have the following items in > my ComboBox: > > IntegerValue StringValue > 3 Apple > 4 Banana > 1 Carrot > 5 Eggplant > 2 Fig > > By doing something like ComboBox.SelectedItem.IntegerValue = 1 the > ComboBox would then display Carrot. > > I imagine I would need to create a custom ComboBox control and add a > function which is kicked off when the IntegerValue is Set, or perhaps > just override an existing function. > > Has anyone done this or something similar before or have any > suggestions? > > Regards, > Mong > Thanks Cor,
I was able to get this working using your suggestion and building an arraylist which I then bind the combo to. Only problem is because the control is bound (I assume) it defaults to selecting index 0 rather than -1, even if the last thing I do is set SelectedItem = -1 it will still display the first item in the list. Is there any way around this? Mongphong,
Bephore we try other things there is a bug in version 1.x that you have to set the selecteditem twice to -1. I have the idea it is in the way you describe it. Cor Cor,
If I use the follwoing: cbo.SelectedIndex = -1 cbo.SelectedIndex = -1 the result is still the same, the first item is displayed. However if I place a button the form with the following in it's click event (and click the button): cbo.SelectedIndex = -1 the combobox does correctly clear with no item displayed. Why do you think it matters where the code is placed? The only difference between the two is that the form has not been shown when the first attempt to set SelectedIndex to -1 is run. Is this the problem? Is it a bug? Is there a fix? Thanks again, Mong Mong,
Did you place it as the last instructions in the Load Event? Cor No it's in the initialisation of the form, that's what I meant in my
last paragraph when I said the form hadn't been shown yet hence the Load event had not been run. So it's a bug then, is there a fix? Obviously I can add cbo.SelectedIndex = -1 to the Load event of the form, but it shouldn't matter if the control is visible or not. Monghponhg,
No it is not a bug, by settting the datasource will the index as well be set. Therefore if you with this, than have you to do this after that setting of the datasource, by instance in the load event of the form. I hope this helps, Cor Cor,
As previously stated the datasource has been set, the combo list has been populated, and the index has been set to -1. However as I also previously stated if this is done before the form has been visually shown ie form.show() has been called and the form load event fired, setting the index to -1 does not work. It will only work if the index is set to -1 after form load has run. As the object has been created during form initialisation (meaning an object of the class has been instantiated), whether it is visible or not should be irrelavent as the object exists. However this situation shows that although other functionality of the ComboBox class works as expected ie. setting the datasource, building the list of items etc, setting the index does not. This my friend has the hallmarks of a bug. I'm more than happy to hear from you any evidence to disprove that this is a bug. I only ask could you please get an english speaking colleague to proof read your response as I have difficulty deciphering your posts and am beginning to think you're from Dagobah. Regards, Mong Cor Ligthert [MVP] wrote: Show quoteHide quote > Monghponhg, > > No it is not a bug, by settting the datasource will the index as well be > set. > > Therefore if you with this, than have you to do this after that setting of > the datasource, by instance in the load event of the form. > > I hope this helps, > > Cor
Can opacity/transparency be adjusted within components?
Extract image dimensions (height, width) from Base64 String? DrawIconEx No tables visible in SQL Server connection Progress bar - No activity Basic How To Question: Class Library or Windows Control Library System tray icon Basic DirectSound question PropertyChanged event with VB.NET (.NET 1.1) reading from a text file problem |
|||||||||||||||||||||||