Cannot set SelectedIndex to -1 in ComboBox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I've narrowed down a problem I've been having filling a combobox with the
results of a query stored in a datatable. I'm setting the ValueMember and
DisplayMember to the relevant column names, and then I want to make sure
nothing is selected.

By default it appears that if you do nothing, the first item (0) is
selected. However, I want to set the SelectedIndex to -1.

If I have a standalone window it works, but if I have a window that has an
mdiparent set, it ignores my attempts to set the SelectedIndex to -1 and
always selects item 0.

(here's my code...)
ComboBox1.DataSource = dt
ComboBox1.ValueMember = "chCountryCode"
ComboBox1.DisplayMember = "chCountryDesc"
ComboBox1.SelectedIndex = -1

I've already tried setting SelectedIndex = -1 twice, but that doesn't fix
it. The only thing that seems to fix it is making the window non-childmdi,
but our application is an MDI style app!

Is this a bug? Any ideas please?

Many thanks,

Steve
 
Steve Dyte said:
ComboBox1.ValueMember = "chCountryCode"
ComboBox1.DisplayMember = "chCountryDesc"
ComboBox1.SelectedIndex = -1

I've already tried setting SelectedIndex = -1 twice, but that doesn't fix
it. The only thing that seems to fix it is making the window non-childmdi,
but our application is an MDI style app!

BUG: ComboBox Does Not Clear When You Set SelectedIndex to -1
<URL:http://support.microsoft.com/?scid=kb;EN-US;327244>
 
Hi, I'm afraid this doesn't fix the problem. I'd already seen this "bug" and
tried the workarounds. It says it affects version 1.0 of the Framework,
whereas we're on v1.1.

If I create a plain window, it works fine. It's only when my window is a
child window of an MDIParent that it ignores my attemps to set SelectedIndex
= -1.

You should be able to recreate this fairly easily...

Create a form with two MenuItems on it...

Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem1.Click

' if I do this it will ignore my SelectedIndex = -1 and set it to 0
Dim x As New Form2
x.MdiParent = Me
x.Show()
End Sub

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click

' if I do this, it will set SelectedIndex = -1 with no problems!!
Dim x As New Form2
x.Show()

End Sub

FYI, Form2 is my test form. It has one combobox, and code in the form load
to get a DataTable, bind it to the combobox and try to set the SelectedIndex
= -1 (see my original post)

Thanks,

Steve
 
Does .NET Framework 1.1 SP1 fix the SelectedIndex = -1 problem?

The list of fixes in .NET Framework 1.1 SP1 at
http://support.microsoft.com/kb/867460 includes the following:

FIX: The value of the SelectedIndex property of the ComboBox control does
not change when you set the text by using the Text property (839616)
http://support.microsoft.com/kb/839616

This in turn has a reference to:
BUG: ComboBox Does Not Clear When You Set SelectedIndex to -1 (327244)
http://support.microsoft.com/kb/327244.

I have had much trouble not knowing whether I have bound incorrectly, have
an error in my own combobox class that displays two columns in the pull-down,
or whether I'm a victim of the many bugs in the ComboBox that seem to stem
back to the SelectedIndex = -1 problem. More likely, I have some combination
of all of these, which makes debugging a nightmare. :-(

Has anyone tried it out SP1 to find out? I'm going to try tonight.
Scott.
 
Hi, we already have Framework 1.1 with SP1 applied.

The problem we have is easily reproducable... It ONLY happens if you have

1) An MDI Parent window
2) Open a MDI Child window from the MDI Parent window (i.e. setting the
MDIParent property of the child window)
3) Have code in the form_load event that fills a combobox by binding it to a
dataset and set the SelectedIndex = -1

If you change Step 2) to load a new instance of the window without setting
the MDIParent property, the problem does not happen and you can quite happily
set SelectedIndex = -1

Thanks,

Steve
 
Steve,

I have the same problem. Did you ever find a solution? Mdi CHild form with
Combo Box and I need it empty (Blank) when form loads.

Thanks AL
 
If I remember it correctly it is a bug in vs2002, because here in my vs2003
it doesn't happen and you should call SelectedIndex = -1 twice

hth Greetz Peter
 
Hi, kind of...

We gave up with the VB combo because of this problem. We ended up using the
Infragistcs UltraComboEditor control instead, and this works just fine - (we
were already using the Infragistics UltraGrid control).

We did log this problem as a "bug" with our Microsoft Support/Sales contact
but never heard back...

Steve
 
Bernie

I have a couple of problems with Kalvin's solution. First off what is a
CategoryCollection, CategoryServer and Category. I can't find these
referrences in help. Also, loading thousands of items, individually into each
combobox is not feasable. It will slow down the application. There has to be
another solution, hopefully without having to "BUY" another ActiveX component.

Thanks
 
Steve,

I found the solution!!! The way to get the Child MDI forms to clear their
combo boxes is the set the SelectedIndex to -1 in the Parent form after
calling the show method for opening the MDI child. I tested it and it seems
to work fine.

Dweezil
 
Back
Top