How to add new items to ComboBox in C# WinForms

Created: 11/06/2013 12:54:12 p.m.

How to add items to our combobox very easy in .Net (C#)

1) First we need to add a combobox to our WinForm, name comboBox1
2) Second in c# code create a class that will our custom combobox item like this:

    public class ComboBoxItem
        public string Value;
        public string Text;

        public ComboBoxItem(string val, string text)
            Value = val;
            Text = text;

        public override string ToString()
            return Text;

3) And finally add some items to our myCombo:

        private void Form1_Load(object sender, EventArgs e)
           // Add item
            comboBox1.Items.Add(new ComboBoxItem("item1 text", "item1 value"));

            // Show item

Remember to puth the custom class after the main clase (public partial class Form1 : Form)

Result: this will show the first item value.

Thanks for reading.


User: Jim Comment date: 21/08/2013 02:00:04 a.m.
User: Quincy Comment date: 24/12/2013 03:32:07 a.m.

database binding with combobox  c# combobox binding


Go to Comments