Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combobox text remains selected after autocomplete + tab

Status
Not open for further replies.

PRMiller2

Technical User
Jul 30, 2010
123
I have a combobox which is populated with a dataset and autocomplete enabled on MainForm_Load:

Code:
        private void FillClientComboBox()
        {
            try
            {
                string conn = ComplianceManager.Properties.Settings.Default.CS_DBConnectionString;
                string sql = "SELECT lngCustomerID_pk, txtCustomerName FROM Customers ORDER BY txtCustomerName;";

                OleDbConnection connection = new OleDbConnection(conn);
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, connection);
                DataSet ds = new DataSet();

                connection.Open();
                dataAdapter.Fill(ds);
                connection.Close();
                clientComboBox.DataSource = ds.Tables[0];
                clientComboBox.DisplayMember = "txtCustomerName";
                clientComboBox.ValueMember = "lngCustomerID_pk";
                clientComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                clientComboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem loading client combo box: " + ex.Message);
            }
        }

The combobox functions as desired: when typing the first few letters of a client, that client can be selected by pressing <TAB> or <ENTER>, the ValueMember and DisplayMember are both changed properly, and the focus is moved to the next control. However, the text from the DisplayMember remains fully highlighted, even while the user types data into another control.

If an item is selected using the dropdown box rather than autocomplete, the appropriate DisplayMember is displayed but does not remain highlighted -- in other words, the desired visual effect.

How do I correct the issue with autocomplete? I have experimented with trapping the tab key on a KeyDown event, but that text remains highlighted.

Thanks,
Paul
 
Have you tried using the LostFocus event to clear the selection? It's not optimal but it should get the job done:
Code:
this.clientComboBox.LostFocus += new EventHandler(clientComboBox_LostFocus);

...

void clientComboBox_LostFocus(object sender, EventArgs e)
{
    this.clientComboBox.Select(0, 0);
}
 
No luck. I pasted that first line ("this.clientComboBox...") towards the bottom of my form's MainForm_Load method, then the LostFocus method you wrote as a separate method. I also set the Focus > Leave event of the combo box in the designer to "clientComboBox_LostFocus" but still no luck.

I am, however, closer to identifying the problem: there is an event that fires on SelectedIndexChanged:

Code:
        private void clientComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            dateTextBox.Focus();
        }

If I comment out the dateTextBox.Focus(); line, the combo box is no longer highlighted when the focus is moved to another control. I tried adding your .Select line to the end of the SelectedIndexChanged method with no luck.

I also tried adding it to the Focus > Enter method for the dateTextBox control, but again no luck.

This one is really confusing!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top