ComboBox.SelectedIndex behavior

J

John Kraft

I am using a combobox in an application that is databound to
a datatable. The visible item in the combobox corresponds
to the job classification of an employee: accountant,
manager, etc.

Due to data entry errors, some employees do not have a
classification in the db. For these entries, I want the
combobox to display nothing. I accomplish this by setting
the combobox's selectedIndex property to -1. In doing so,
I encountered some interesting behavior.

setting SelectedIndex = -1 always sets the SelectedIndex to
0, UNLESS the SelectedIndex is already 0 or -1.

(example)
myComboBox.SelectedIndex = -1;

produces a SelectedIndex of 0.

myComboBox.SelectedIndex = -1;
myComboBox.SelectedIndex = -1;

or

myComboBox.SelectedIndex = -0;
myComboBox.SelectedIndex = -1;

produces a SelectedIndex of -1.

Is this the expected behavior of this control, or is this a
bug in the control?

I'm using VS.NET 2003 and .NET 1.1.

Thank you,

John Kraft
 
B

Bart Mermuys

Hi,

John Kraft said:
I am using a combobox in an application that is databound to
a datatable. The visible item in the combobox corresponds
to the job classification of an employee: accountant,
manager, etc.

Due to data entry errors, some employees do not have a
classification in the db. For these entries, I want the
combobox to display nothing. I accomplish this by setting
the combobox's selectedIndex property to -1. In doing so,
I encountered some interesting behavior.

setting SelectedIndex = -1 always sets the SelectedIndex to
0, UNLESS the SelectedIndex is already 0 or -1.

(example)
myComboBox.SelectedIndex = -1;

produces a SelectedIndex of 0.

myComboBox.SelectedIndex = -1;
myComboBox.SelectedIndex = -1;

or

myComboBox.SelectedIndex = -0;
myComboBox.SelectedIndex = -1;

produces a SelectedIndex of -1.

Is this the expected behavior of this control, or is this a
bug in the control?

Yes, it is a known problem in NET1.1, fixed in NET2.0.

If you are interrested it goes like this:
When you set SelectedIndex to -1, the ComboBox tries to set
CurrencyManager.Position to -1, but that can't be done, so the CM.Position
goes to 0. CM.PositionChanged fires because of the change and the ComboBox
sees this and updates SelectedIndex to 0.
Now, if you repeat this, the ComboBox again tries to set CM.Position to -1,
this fails and the CM.Position remains at 0, but no CM.PositionChanged event
occured and the ComboBox.SelectedIndex remains at -1.

At the end i pasted a Control that inherits from ComboBox to fix the
problem.

HTH,
Greetings

Public Class NComboBox
Inherits System.Windows.Forms.ComboBox
Public Event SelectedIndexChanged2 As EventHandler

Protected Overrides Sub OnSelectedIndexChanged(ByVal e As EventArgs)
' fire selectedvalue changed
OnSelectedValueChanged(EventArgs.Empty)

' fire selectedindex changed
RaiseEvent SelectedIndexChanged2(Me, e)

' fix, only update currencymanager if index<>-1
If ((Not MyBase.DataManager Is Nothing) AndAlso _
(MyBase.DataManager.Position <> SelectedIndex) AndAlso _
(SelectedIndex <> -1)) Then
MyBase.DataManager.Position = SelectedIndex
End If
End Sub

End Class
 

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