I am using a few combo boxes in my form and have set the DropDownStyle property set to DropDownList. I want to expand i.e. show ALL options when the control gets focus (similar to presing the F4). Also some entries of my list have a greater width then the width at design time. I want to display the entire entries based on the width when the control gets focus. I also want to implement autocomplete feature in my combo boxes so that the user can continue typing and only matching entries gets displayed. But when I type the second alphabet the entry which begins with that alphabet is getting selected. I have done all this and more in VisualBasic 6 using API calls, but when it comes to C# 2005, I am clueless. I request members in helping me with this.

Most of the Comboboxes of my form have fixed entries, but in some others I want to display unique values from a table. How do I go about this?

Thank you.

Lalit Kumar Barik
India

Dani AI

Generated

Useful, practical patterns for C# WinForms (VS2005/.NET 2.0) that address each part of the original question. pointed out sizing and data-binding are important — the snippets below give concrete, reliable ways to (1) open the dropdown on focus, (2) let the dropdown expand to show long items, (3) enable true autocomplete vs. the built-in type-selection, and (4) populate the box with unique values from a database.

Open the drop-down on focus (do this in Enter and use BeginInvoke to avoid reentrancy/flicker):

private void comboBox1_Enter(object sender, EventArgs e)
{
    this.BeginInvoke((MethodInvoker)delegate { comboBox1.DroppedDown = true; });
}

Make the drop-down wide enough for the longest display text (call once after loading/binding):

private void AdjustDropDownWidth(ComboBox cb)
{
    int max = cb.DropDownWidth;
    using (Graphics g = cb.CreateGraphics())
    {
        for (int i = 0; i < cb.Items.Count; i++)
        {
            string s = cb.GetItemText(cb.Items[i]);
            int w = TextRenderer.MeasureText(g, s, cb.Font).Width;
            if (w > max) max = w;
        }
    }
    cb.DropDownWidth = max + SystemInformation.VerticalScrollBarWidth;
}

Autocomplete vs. filtering: built-in AutoComplete (Suggest/Append) requires an editable control (DropDownStyle = DropDown) and uses AutoCompleteCustomSource. To actually filter visible items as the user types (reduce the list to matching rows) bind to a DataView and set RowFilter in TextChanged — then re-open the dropdown and restore caret position:

private DataView dv;
private void Setup(DataTable dt)
{
    dv = dt.DefaultView;
    comboBox1.DataSource = dv;
    comboBox1.DisplayMember = "Name";
}

private void comboBox1_TextChanged(object sender, EventArgs e)
{
    dv.RowFilter = "Name LIKE '" + comboBox1.Text.Replace("'", "''") + "%'";
    this.BeginInvoke((MethodInvoker)delegate { comboBox1.DroppedDown = true; });
    comboBox1.SelectionStart = comboBox1.Text.Length;
}

Get unique values from the DB server-side (preferred) with SELECT DISTINCT, or client-side with DataView.ToTable(true, "ColumnName"). Example (SqlClient):

DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connString))
using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT Name FROM MyTable ORDER BY Name", conn))
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
    da.Fill(dt);
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = dt;

References: ComboBox.DroppedDown and AutoComplete properties are documented by Microsoft; filtering via DataView.RowFilter is a standard approach. For very large lists, prefer server-side distinct/select or incremental search queries instead of loading everything into memory.

There's a few questions in there.

"I want to expand i.e. show ALL options when the control gets focus" - You could put something in the paint event looping through the values and use measureString to get the width. This could get processing expensive. If the values are static after the form loads, it would be better to just do an initialization and set the sizes once.

'I want to display the entire entries based on the width when the control gets focus' - I don't understand this need. Coupled with the first questions, the comboBox control does this natively.

Your last question about filtering, I think the way to do it is to use a datatable and bind the table to the comboBox. Then use the table to do the filtering.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.