views:

919

answers:

2

Let's say we have the following code in a Windows application:

ComboBox comboBox = new ComboBox()
{
    AutoCompleteMode = AutoCompleteMode.SuggestAppend,
    AutoCompleteSource = AutoCompleteSource.ListItems,
    DataSource = new string[] { "", "Ark", "Boat", "Bucket" },
    DropDownStyle = ComboBoxStyle.DropDownList
};
this.Controls.Add(comboBox);

TextBox textBox = new TextBox()
{
    Left = comboBox.Right,
    Top = comboBox.Top,
    ReadOnly = true
};
textBox.DataBindings.Add("Text", comboBox, "SelectedValue");
this.Controls.Add(textBox);

No magic here, just a ComboBox bound to a list of strings. The TextBox displays the SelectedValue of the ComboBox.

I'm getting unexpected behavior when I type "Bucket" into the ComboBox and tab away. For some reason the ComboBox displays "Boat" but the TextBox displays "Bucket". I would expect them both to display "Bucket".

It behaves as expected if I change the DropDownStyle to DropDown, but I don't want users to be able to type anything they want. They should only be able to type items that are in the list.

Even more interesting is that, after typing "Bucket" and tabbing away, if I type "Bucket" again it will display "Bucket" in both. If I make a third attempt, it goes back to "Boat" for the ComboBox and "Bucket" for the `TextBox'. So it seems like it's cycling through all the B's.

I didn't notice this until I upgraded from XP to Windows 7 recently. I don't see how that could have anything to do with this, but I'm throwing it out anyway.

If this behavior is correct, can anyone tell me what I should be doing to achieve my expected behavior?

UPDATE It would seem this is related to Windows 7. Everything behaves as expected in Windows XP Mode. Can anyone else running Windows 7 try my code and verify that I'm not crazy?

+2  A: 

A workaround could be changing the DropDownStyle to DropDown and adding the following:

comboBox.Validating += new CancelEventHandler((o, e) =>
    {
        e.Cancel = (comboBox.DataSource as string[]).Contains(comboBox.Text) == false;
    });

That will allow users to type anything, but it won't let them tab away from the control unless they typed a valid item.

Still, not happy with the behavior changing from XP to Win 7.

Ecyrb
A: 

I know that this response is quite old, but I was requiring an answer to this Windows 7 bug. I tinkered around for a while in the vein of Ecyrb and came up with the following work-around:

From InitForm() for Application Add this property:

Me.KeyPreview = True

From where the ComboBox is located:

Private mbTab As Boolean 
Private miPrevIndex As Integer = -1
Private Sub DropDownListEx_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Validating
   miPrevIndex = Me.SelectedIndex
   MyBase.OnSelectedIndexChanged(e)
End Sub
Private Sub DropDownListEx_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Me.PreviewKeyDown
   mbTab = e.KeyCode = Windows.Forms.Keys.Tab
End Sub
Protected Overrides Sub OnDropDownClosed(ByVal e As System.EventArgs)
    MyBase.OnDropDownClosed(e)
    If Me.SelectedIndex <> miPrevIndex Then
        If mbTab = True Then
            Me.SelectedIndex = miPrevIndex
        Else
            miPrevIndex = Me.SelectedIndex
        End If
        MyBase.OnSelectedIndexChanged(e)
    End If
End Sub

Now, in my example, I am using a custom control that inherits comboBox. So you will need to wire these up for your own use.

websch01ar