组合框的WinForms失去对引发LostFocus自动完成值 [英] Winforms combobox loses autocomplete value on lostfocus

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

问题描述

我有同的WinForms组合框失去了自动完成过程中发现的价值问题,当用户按Tab键下一个控件。

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);
}

为了重现bug,请执行下列操作步骤:

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


  1. 运行测试
    中的表格会弹出组合框的重点...

  2. 现在,键入'bbb3选择具有自动完成相应的项目。
    现在,您将看到文本框一直与bbb3作为您选择的产品的更新。

  3. 现在,按TAB键

您现在看到的文本框具有焦点和组合选择已更改为bbb1。
还要注意,在文本框中它表明你所选择的价值却依然是bbb3当Leave事件被解雇,但它是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

  • 点击项目


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

任何想法?

推荐答案

我发现从微软这个链接

http://connect.microsoft.com/VisualStudio/feedback/details/711945/tab-on-a-winforms-combobox-with-properties-autocompletemode-append-autocompletesource -listitems-犯规-工作正确

基本上,这是一个已知的问题,微软表示,他们将无法修复。但也有在该链接的解决方法部分有两个解决方法。

Basically this is a known issue that microsoft say they will not fix. However there are two workarounds under the workarounds section of that link.

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

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