Click here to Skip to main content
15,902,745 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
private List<string> items;


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

C#
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)
               {

               }
           }
       }


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

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


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

}
Posted

1 solution

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.
C#
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();
}
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900