TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# ComboBox: Windows Forms

This C# article demonstrates the ComboBox control. It covers ComboBox properties and event handlers.

ComboBox is a combination TextBox with a drop-down. Its drop-down list presents preset choices.

The user can type anything into the ComboBox. Alternatively, he or she can select something from the list.

TextBox

Example. To begin, please create a new Windows Forms application and add a ComboBox to it. You can then right-click on the ComboBox and add the SelectedIndexChanged and TextChanged event handlers in the Properties dialog.

This program shows how when you change the text by typing in the ComboBox or by clicking on the list of Items, the event handlers are triggered. Please see the section on the Items property before running the program.

C# program that demonstrates ComboBox event handlers

using System;
using System.Windows.Forms;

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

	int _selectedIndex;
	string _text;

	private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
	{
	    // Called when a new index is selected.
	    _selectedIndex = comboBox1.SelectedIndex;
	    Display();
	}

	private void comboBox1_TextChanged(object sender, EventArgs e)
	{
	    // Called whenever text changes.
	    _text = comboBox1.Text;
	    Display();
	}

	void Display()
	{
	    this.Text = string.Format("Text: {0}; SelectedIndex: {1}",
		_text,
		_selectedIndex);
	}
    }
}

Text. The Text property of the ComboBox functions much like the Text property of a TextBox. If you assign to the Text property, the current value in the ComboBox will change. You can also read the Text property and assign string variables to it.

Tip: To clear it, you can assign it to an empty string literal. This is an effective way to clear many properties.

Empty String ExamplesString Literal

Items. The Items property contains the strings that are found in the drop-down part of the ComboBox. You can type the Items line-by-line into Visual Studio through the Items dialog, or you can dynamically add them during runtime.

Tip: To add Items, call the Add method: use the syntax comboBox1.Add("Value"). You can also Clear the ComboBox with Clear().

Dialog usage. Conceptually, the ComboBox is used to represent a text input with a set of associated, predefined values that are easy to select. For this reason, it is a good choice for preference dialogs.

You can use ComboBox controls and have preset values that are present in the drop-downs, but allow your users to select any value by typing it in directly. This avoids the need for more than one control.

AutoComplete. There are three properties for AutoComplete on the ComboBox: the AutoCompleteCustomSource, the AutoCompleteMode, and the AutoCompleteSource. The AutoCompleteMode property can be set to suggest, append or both.

And: Source properties let you specify the set of strings that are used as suggestions.

DropDown styles. There are three DropDown style properties. They are DropDownHeight, DropDownStyle and DropDownWidth. The DropDownHeight and DropDownWidth properties seem not to affect the visual appearance. Windows uses its default widgets.

Also: You can remove the drop-down entirely (with Simple), or make it so the text is not editable (with DropDownList).

MaxDropDownItems. The MaxDropDownItems property had no effect in my testing. In a ComboBox with three items, setting a MaxDropDownItems of one did not reduce the number of items. It may be effective only when DropDownStyle is set to a non-default value.

 

Summary. By combining the TextBox and a regular drop-down list, the ComboBox control represents a truly useful hybrid widget in Windows Forms. It is ideal for dialogs where some suggestions for an input may be known, but any value must be accepted.

Review: The ComboBox can streamline your interface by coalescing multiple user interface controls.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf