articles

Home / DeveloperSection / Articles / ComboBox Control in C#.Net

ComboBox Control in C#.Net

Pushpendra Singh17144 24-Jan-2011

ComboBox control is a combination of a TextBox and a ListBox.

How to use ComboBox

Drag and drop ComboBox from toolbox on window Form.

ComboBox Control in C#.Net

Code:

using System;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }
        private void Form6_Load(object sender, EventArgs e)
        {
            // Item is added in combobox
            comboBox1.Items.Add("C#");
            comboBox1.Items.Add("J#");
            comboBox1.Items.Add("VB");
            comboBox1.Items.Add("Java");
        }
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Selected value will show in Label
            label1.Text = "Selected Language is " +    comboBox1.SelectedItem.ToString();
        }
    }
}

Run the project

ComboBox Control in C#.Net

When you select the given option then SelectedIndexChanged event will fire and selected item will show in the Label.

ComboBox Control in C#.Net

AutoComplete ComboBox

Choose AutoCompleteSource property of ComboBox is ListItem and also set AutoCompleteMode to suggest, append, or SuggestAppend.

ComboBox Control in C#.Net

When you type any text in the ComboBox then it searches the related item and if related item is available then it suggest the related item.

ComboBox Control in C#.Net

ComboBox Properties:

AutoCompleteMode: Defines an option that controls how automatic completion works for the ComboBox.

AutoCompleteSource:  Defines a value specifying the source of complete strings used for automatic completion.

BackColor: BackColor of ComboBox is changed through BackColor property.

Example:

private void Form6_Load(object sender, EventArgs e)
{
     //change backcolor of comboBox
     comboBox1.BackColor = Color.CadetBlue;
}

Output

ComboBox Control in C#.Net


Updated 04-Mar-2020

Leave Comment

Comments

Liked By