单击Windows窗体组合框自动完成建议返回null /空白 [英] Windows Form Combobox Auto complete suggestion on click returns null / blank

查看:60
本文介绍了单击Windows窗体组合框自动完成建议返回null /空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI全部,



我有一个组合框(CB),通过CB.Datasource列表填充



CB已填充,但当任何用户点击建议的自动完成时,快速闪烁CB.Text然后将CB.Text设置为空值或为空。



如果在CB中的选定值字段上使用制表符和箭头键,则它可以完美地工作。有没有办法或解决方案我可以修复它所以点击建议的自动完成CB.Text填充选定的值。



HI All,

I have a Combo Box (CB) which is being populated from a list through CB.Datasource

The CB is populated but when any user clicks on the suggested auto complete the flashes quickly CB.Text then sets the CB.Text to a null value or empty.

If tab and arrow keys are used on the selected value field in the CB it works perfectly. Is there a way or solution anyone I can fix it so on clicking the suggested autocomplete the CB.Text fills with the selected value.

private List<string> items;





设置为Active Directory查询用户名并通过此方法将它们添加到列表中。





Its set to query Active Directory for usernames and add them to the list through this method.

private void findAllUsers()
       {
           items = new List<string>();

           PrincipalContext corp = new PrincipalContext(ContextType.Domain);
           UserPrincipal uName = new UserPrincipal(corp);
           PrincipalSearcher srch = new PrincipalSearcher(uName);

           foreach (var found in srch.FindAll())
           {
               UserPrincipal foundUser = found as UserPrincipal;
               try
               {
                   if (foundUser != null)
                   {
                       items.Add(foundUser.SamAccountName);
                   }

               }

               catch (Exception)
               {

               }
           }
       }





然后在表单加载时通过后台工作程序加载项目。 br $>




Items are then loaded through a background worker on form load.

public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
     {
         findAllUsers();
     }







private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    comboBox1.DataSource = items;
    comboBox1.SelectedItem = null;

}

推荐答案

首先:了解ComboBox由TextBox和List组成(内部,这些都是Type'ComboBoxChildNativeWindow,它继承自'NativeWindow)。



当用户点击TextBox时,ComboBox的标准行为不是触发选择事件,它是设置焦点...将编辑/插入光标放在TextBox中。你应该有一个很好的理由来改变这种标准行为,因为:这是用户通过经验期望社交的东西。



注意:什么你说'Tab键让我感到困惑:据我所知,Tab键对ComboBox没有任何独特之处。



这样的大多数问题都是因为没有将ComboBox.AutoCompleteMode设置为'AutoComplete.SuggestAppend,或'AutoComplete.Append。并且,没有将'AutoCompleteSource设置为AutoCompleteSource.Custom。



以下是它的工作原理:将一个ComboBox,'comboBox1'放在WinForm项目的主窗体上,放一个TextBox,'表格上的textBox1。
First: understand that a ComboBox is composed of a TextBox and a List (internally, these are both Type 'ComboBoxChildNativeWindow which inherits from 'NativeWindow).

The standard behavior of a ComboBox when the user clicks in the TextBox is not to trigger a Selection Event, it is to set the focus ... place the edit/insertion cursor ... in the TextBox. You should have a very good reason to change that standard behavior because: that's what users are socialized through experience to expect.

Note: what you say about the 'Tab Key puzzles me: to my knowledge the Tab Key does not do anything unique with a ComboBox.

Most problems like this are the result of not setting the ComboBox.AutoCompleteMode to 'AutoComplete.SuggestAppend, or 'AutoComplete.Append. And, not setting the 'AutoCompleteSource to AutoCompleteSource.Custom.

Here's how it works: Put a ComboBox, 'comboBox1, on a main Form in a WinForm Project, put a TextBox, 'textBox1 on the Form.
string[] data = new string[]
{
    "Absecon","Abstracta","Abundantia","Academia","Acadiau",
    "Acamas","Ackerman","Ackley","Ackworth","Acomita","Aconcagua",
    "Acton","Acushnet","Acworth","Ada","Ada","Adair","Adairs","Adair",
    "Adak","Adalberta","Adamkrafft","Adams"};
}

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
    AutoCompleteStringCollection aCompl = new AutoCompleteStringCollection();
    aCompl.AddRange(data);
    comboBox1.Items.Clear();
    comboBox1.DataSource = data;
    comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
    comboBox1.AutoCompleteCustomSource = aCompl;
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs eventArgs)
{
    textBox1.Text = comboBox1.SelectedItem.ToString();
}

有三种类型的用户操作会导致'SelectedIndexChanged事件发生:



1.用户开始输入,文本字段中出现一个自动完成条目,用户点击'返回键



2.用户下拉项选择器列表并单击从该列表中选择



3.用户开始输入,出现自动完成条目,用户使用箭头键在项目选择器列表中向下滚动,当用户点击'返回键时,选择列表中当前高亮的项目被选中

There are three types of user-action that will cause the 'SelectedIndexChanged Event to occur:

1. the user starts typing, an auto-complete entry appears in the text field, the user hits the 'Return key

2. the user drops down the Item selector List and clicks a choice from that List

3. the user starts typing, an auto-complete entry appears, the user uses the arrow-keys to scroll down in the Item selector List, and when the user hits the 'Return key, the current high-lighted Item in the selector List is selected


这篇关于单击Windows窗体组合框自动完成建议返回null /空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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