By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,703 Members | 1,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,703 IT Pros & Developers. It's quick & easy.

Help creating custom ComboBox control

P: n/a
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

Feb 20 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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?...c-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
<mo*********@yahoo.com.au> schreef in bericht
news:11*********************@z14g2000cwz.googlegro ups.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

Feb 20 '06 #2

P: n/a
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?

Feb 21 '06 #3

P: n/a
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
Feb 21 '06 #4

P: n/a
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

Feb 22 '06 #5

P: n/a
Mong,

Did you place it as the last instructions in the Load Event?

Cor
Feb 22 '06 #6

P: n/a
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.

Feb 23 '06 #7

P: n/a
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
Feb 23 '06 #8

P: n/a
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:
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


Mar 6 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.