Go to Comments





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#)
WinForms


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
            MessageBox.Show(((ComboBoxItem)comboBox1.Items[0]).Value);
        }


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.
maksim20032@hotmail.com






Comments:

User: Jim Comment date: 21/08/2013 02:00:04 a.m.
http://www.kettic.com/winforms_ui/multi_column_combobox.shtml
User: Quincy Comment date: 24/12/2013 03:32:07 a.m.

database binding with combobox http://csharp.net-informations.com/dataset/dataset-combobox.htm  c# combobox binding

quincy

User: name Comment date: 27/08/2014 09:50:56 a.m.