C# Exercises

Home AgriMetSoft About Contact

Add Item to Combobox in C#

	
namespace Add_item_to_combobox
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void add_Click(object sender, EventArgs e)
    {
      comboBox1.DataSource = null;
      comboBox1.Text = "Select Items";
      if (comboBox1.Items.Count > 0)
        comboBox1.Items.Clear();
      comboBox1.Items.Add("Item 1");
      comboBox1.Items.Add("Item 2");
      comboBox1.Items.Add("Item 3");
      comboBox1.SelectedIndex = 1;
    }

    private void link_Click(object sender, EventArgs e)
    {
      if (comboBox1.Items.Count > 0)
        comboBox1.Items.Clear();
      var personList = new List<person>();
      personList.Add(new person { name = "rob", age = 32 });
      personList.Add(new person { name = "annie", age = 24 });
      personList.Add(new person { name = "paul", age = 19 });
      comboBox1.DataSource = personList;
      comboBox1.DisplayMember = "name";
    }
    class person
    {
      public string name { get; set; }
      public int age { get; set; }
    }
   
  }
}
	


Download the project of Visual Studio 2013 in DropBox Download


Add Item to Combobox with Example of Windows Form Application C# | Link to List


List of Exercises