I have a combo box. I want to make it editable on click of an item so that user can add new item to the combo box.

Please find the code below.

private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1 = new ComboBox();
            comboBox1.Location = new Point(20, 60);
            comboBox1.Name = "comboBox1";
            comboBox1.Size = new Size(245, 25);

            comboBox1.Items.Add("A");
            comboBox1.Items.Add("B");
            comboBox1.Items.Add("C");
            comboBox1.Items.Add("D");
            comboBox1.Items.Add("Add");

            this.Controls.Add(comboBox1);
            comboBox1.DrawItem += comboBox1_DrawItem;
            comboBox1.SelectedValueChanged += OnComboBox1SelectionChanged;
        }

        private void OnComboBox1SelectionChanged(object sender, EventArgs e)
        {
            if (((ComboBox)sender).SelectedItem.ToString() == "Add")
            {
                //
            }
        }

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            if (e.Index > -1)
            {
                e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
            }
        }

    private void Save_Click(object sender, EventArgs e)
        {

        }

Here, if the user clicks "Add", i want to make the combobox editable which will actualy let the user type a new data in the combo box and
on clicking "Save", add the item to the combo box and make it readable. user should be able to Add as many items as they wish to.

Can we do that. I googled but could'nt find.
Please let me know how we can do that.

Dani AI

Generated

A simple, reliable way to let the user type a new item when they pick the "Add" entry is to temporarily switch the ComboBox from read-only (DropDownList) to editable (DropDown), let them enter text, then add the text on Save and restore the original style. That avoids complex owner-draw or key handling and matches the goal in 's post. 's _addingItem idea is a fine pattern for guarding state; this approach complements it by handling the control style and insertion point.

A minimal pattern (hook the selection event and the Save button):

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedItem?.ToString() == "Add")
    {
        comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
        comboBox1.Text = "";
        comboBox1.Focus();
        comboBox1.SelectAll();
    }
}

private void btnSave_Click(object sender, EventArgs e)
{
    string text = comboBox1.Text.Trim();
    if (text.Length == 0) return;

    if (!comboBox1.Items.Contains(text))
    {
        int addIndex = comboBox1.Items.IndexOf("Add");
        int insertAt = addIndex >= 0 ? addIndex : comboBox1.Items.Count;
        comboBox1.Items.Insert(insertAt, text);
        comboBox1.SelectedIndex = insertAt;
    }

    comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
}

Notes and gotchas:

  • If you use owner-draw, set comboBox1.DrawMode = DrawMode.OwnerDrawFixed (otherwise your DrawItem handler won’t be called). Handle e.Index == -1 cases appropriately.
  • Changing DropDownStyle or SelectedIndex programmatically can re-fire selection events; use a small guard flag (as suggests) or use SelectionChangeCommitted to detect only user actions.
  • Keep the "Add" sentinel at the end (insert new items before it) and check for duplicates before adding.
  • For better UX, consider a small input dialog or overlay TextBox instead of changing the ComboBox style if the visual jump feels jarring.

Hmm, seems awkward. I'd probably start by trying something like this:

private bool _addingItem = false;

private void comboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
    var ctrl = sender as ComboBox;

    if (ctrl.SelectedItem.ToString() == "Add")
    {
        _addingItem = true;
        ctrl.Focus();
    }
}

private void comboBox_KeyDown(object sender, KeyEventArgs e)
{
    if (!_addingItem)
    {
        return;
    }

    var ctrl = sender as ComboBox;

    if (e.KeyCode == Keys.Enter)
    {
        if (!ctrl.Items.Contains(ctrl.Text))
        {
            ctrl.Items.Add(ctrl.Text);
            ctrl.Invalidate();
            e.Handled = true;
        }
    }
}

private void comboBox_Leave(object sender, EventArgs e)
{
    _addingItem = false;
}

Obviously there are plenty of use cases to consider, but for the basic functionality the above seems like a reasonable start.

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.