ComboBox in C#

Download
Download is available until [expire_date]
  • Version
  • Download 38
  • File Size 44.24 KB
  • File Count 1
  • Create Date April 25, 2016
  • Last Updated April 25, 2016

ComboBox in C#

ComboBox in C#

This is a sample program in c# that shows how to load items in combo box. The program has two combo boxes, the first is the main category combo box and the second is the sub category combo box. Every category has its own sub item, to display the sub items in sub category combo box a main item must be selected first in the main category combo box

Example: Main Combo Box selected is Color, then the items that will be displayed in the sub category combo box will be; Blue, Red, Yellow.

A message box will be displayed (the main item and the sub item selected) based on what item you have selected.

Source code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("Color");
            comboBox1.Items.Add("Animal");
            comboBox1.Items.Add("Country");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "Color")
            {
                comboBox2.Items.Clear();
                comboBox2.Items.Add("Blue");
                comboBox2.Items.Add("Red");
                comboBox2.Items.Add("Yellow");           
            }
            else if (comboBox1.Text == "Animal")
            {
                comboBox2.Items.Clear();
                comboBox2.Items.Add("Dog");
                comboBox2.Items.Add("Cat");
                comboBox2.Items.Add("Horse");        
            }
            else if (comboBox1.Text == "Country")
            {
                comboBox2.Items.Clear();
                comboBox2.Items.Add("US");
                comboBox2.Items.Add("China");
                comboBox2.Items.Add("Russia");
            }

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(comboBox1.Text + ": " + comboBox2.Text);
        }
    }
}
, , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.