Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

c# - Winforms combobox loses autocomplete value on lostfocus

I am having issues with the Winforms combo box losing the value found during an autocompletion when the user tabs to the next control.

Here is a code sample (as an Nunit Test that will pop up a form):

[Test]
[STAThread]
public void Testing_AsDropDownList()
{
    var comboBox = new ComboBox();
    comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
    comboBox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
    comboBox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
    comboBox.Items.Add(new ComboPair("aaa", "ItemAAA"));
    comboBox.Items.Add(new ComboPair("bbb1", "ItemBBB1"));
    comboBox.Items.Add(new ComboPair("bbb2", "ItemBBB2"));
    comboBox.Items.Add(new ComboPair("bbb3", "ItemBBB3"));
    comboBox.Items.Add(new ComboPair("ccc", "ItemCCC"));
    var textBox = new TextBox{ Multiline = true };        
    comboBox.Leave += (sender, args) => textBox.Text = "On Leave: " + comboBox.SelectedItem;
    comboBox.LostFocus += (sender, args) => textBox.Text += " ... On LostFocus: " + comboBox.SelectedItem;
    var frm = new Form();
    frm.Width = 300;
    frm.Height = 100;
    comboBox.Dock = System.Windows.Forms.DockStyle.Top;
    textBox.Dock = System.Windows.Forms.DockStyle.Bottom;
    frm.Controls.Add(comboBox);
    frm.Controls.Add(textBox);
    Application.EnableVisualStyles();
    Application.Run(frm);
}

In order to reproduce the bug, do the following steps:

  1. Run the test The form will pop up with the combo box focused...
  2. Now type 'bbb3' to select the corresponding item with the autocompletion. You will now see that the text box has been updated with 'bbb3' as your selected item.
  3. Now press TAB

You will now see that the text box has the focus and the combo selection has changed to 'bbb1'. Also note that in the the text box it shows you that the selected value was still 'bbb3' when the leave event was fired, but then it was 'bbb1' when the lost focus event fired.

This same behaviour is seen if you click away from the combo box to make it loose focus for step 3.

If you do anything else at step 3 it won't have this problem. i.e. if you:

  • press 'enter'
  • press 'up' then 'down' to get back to "bbb3"
  • click the item
  • etc.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The value gets lost at the WM_KILLFOCUS message. Overriding WndProc in a subclass of ComboBox solved this issue for me (except for the clicking to loose focus... but I guess this could be explained as dismissing like on a dialog of a website). Unfortunately, I have only VB.NET-code at hand:

Protected Overrides Sub WndProc(ByRef m As Message)
    If m.Msg = &H8 Then  'WM_KILLFOCUS
        Dim sText As String = Me.Text
        MyBase.WndProc(m)
        Me.Text = sText
        Exit Sub
    End If

    MyBase.WndProc(m)
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...