Winforms 组合框在 lostfocus 上失去自动完成值 [英] Winforms combobox loses autocomplete value on lostfocus

查看:18
本文介绍了Winforms 组合框在 lostfocus 上失去自动完成值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户切换到下一个控件时,我遇到了 Winforms 组合框丢失在自动完成过程中找到的值的问题.

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

这是一个代码示例(作为一个会弹出表单的 Nunit 测试):

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. 运行测试表单将弹出并聚焦组合框...
  2. 现在输入bbb3"以选择具有自动完成功能的相应项目.您现在将看到文本框已更新为bbb3"作为您选择的项目.
  3. 现在按 TAB

您现在将看到文本框具有焦点并且组合选择已更改为bbb1".另请注意,在文本框中,它显示在触发 leave 事件时所选值仍为 'bbb3',但在触发丢失焦点事件时为 'bbb1'.

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.

如果您在组合框外单击以使其在步骤 3 中失去焦点,则会看到相同的行为.

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

如果您在第 3 步执行其他操作,则不会出现此问题.即如果你:

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

  • 按回车"
  • 按向上"然后向下"返回bbb3"
  • 点击项目

有什么想法吗?

推荐答案

该值在 WM_KILLFOCUS 消息处丢失.在 ComboBox 的子类中覆盖 WndProc 为我解决了这个问题(除了单击以松开焦点......但我想这可以解释为像在网站的对话框中一样解散).不幸的是,我手头只有 VB.NET 代码:

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

这篇关于Winforms 组合框在 lostfocus 上失去自动完成值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆