Help creating custom ComboBox control

M

mongphong28

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
 
M

mongphong28

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?
 
C

Cor Ligthert [MVP]

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
 
M

mongphong28

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
 
M

mongphong28

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.
 
C

Cor Ligthert [MVP]

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
 
M

mongphong28

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top