Databinding a combo box

J

Jeff Mason

I'm observing some strange behavior when I use a bound combo box in conjunction with
the combo's anchor property. I define a form which contains just a textbox and a
combo box. The text box is placed before the combo in taborder, and is there only so
the focus will go someplace other than the combo box when the form is displayed.

In the form load event, I have the following code to create an arraylist and bind the
combo to it:

Dim a As ArrayList = New ArrayList

For i As Integer = 1 To 20
a.Add("Test Line " & i)
Next

ComboBox1.DataSource = a

When the form is displayed, the first item in the combo box's list is displayed in
the combo's associated textbox. If you set the combo's anchor property to any other
value than "Top, Left", the combo's text is displayed and also selected, even though
the combo does not have the focus.

If I load the combo "by hand", manually adding the above strings to the combo's Items
collection, no item is selected when the form is displayed, regardless of the anchor
property.

Anybody have any idea why the combo would choose to select text with any anchor value
other than "top, left" but not select it if it is "top, left"? Ad what does the
anchor property have to do with this, anyway?

I realize I can set the combo's SelectedIndex value to -1 after setting the
datasource, but I believe the behavior I am observing is inconsistent. Is this a bug
or is there something I don't understand?
 
C

Cor Ligthert

Hi Jeff,

Most of us stopped looking at strange behaviour in the initialization fase
of the combobox.

However preventing the selectedindexchange to start, with a bool/switch/flag
which you set to true at the end of your load, will mostly overcome most
problems (not all).

I hope it helps anyway?

Cor
 

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