JavaScript Error

You currently have JavaScript disabled on your web browser.

This website uses JavaScript, and This web page needs JavaScript activated to work correctly.

Please active JavaScript on your web browser and then refresh this web page.



ComboBox



example1

The ComboBox control is found in the System.Windows.Forms namespace within the System.Windows.Forms.dll. In this blog post I am referring to the ComboBox control that is available in C# .NET-Core (.NET 6.0) with Visual Studio 2022 (although this code example might still work OK with older .NET versions).

The purpose and function of the ComboBox is to display a text box combined with a ListBox, which enables the user to select items from its drop down list or enter a new value into its text box.

You can change the way the ComboBox control looks on the form by altering the DropDownStyle property, so that its list is either always displayed or displayed in a drop-down. The DropDownStyle property also specifies whether the text portion is ReadOnly or if it can be edited. There is no setting to always display the list and disallow entering a new value (which is what a ListBox control is).

Items can be added or removed to the ComboBox control both at design time and at run time. You can assign an array of object references to add a group of items at once by using the AddRange method, which then displays the default string value for each object. You can add individual objects with the Add method. With the BeginUpdate and EndUpdate methods, you can add a large number of items without the control being repainted each time an item is added to the list. You can delete items with the Remove method, or you can clear all items from the entire list with the Clear method.

The ComboBox also provides features that enable you to efficiently add items and find text within the items of the list. The FindString and FindStringExact methods enable you to search for an item in the list that contains a specific search string.


Example Source Code

This example uses a ComboBox control, along with two Label controls and a TextBox control.

To add the ComboBox control to your form, you can double click on its name (i.e. ComboBox) as listed in the Toolbox window panel within the Form editor window. Alternatively, you can single click on it and then drag and drop it onto your form, to position it more closer to where you want it to be positioned at. Once it is added to the form then it will appear on the forms surface area having default ComboBox control values.

After you have added the ComboBox control to your form, then once you select it then you can view and edit that objects property values in the Properties window within the Forms editor window, where you can then change the controls Name, Items, and other properties as you desire.

In the following example, I added two Label objects to the Form, and then changed one of the Labels Text property to have a value of "Favorite Pet:" and the other Labels Text property to have a value of "Info:". I then added a ComboBox and positioned it be next to the first Label, and then added a TextBox and positioned it below the second Label. I adjusted the TextBox properties so that it is MultiLined, ReadOnly, and Scrollbars Visible:

example2

From the Form editor, I select the control, view its Events properties in the Property Window, and then double clicked on the SelectedValueChanged and on the TextChanged events. Doing so automatically creates and links callback methods to that controls event into that forms .cs source code file, which I can then program some action to be performed whenever the user changes the value. In this example I use both events so that one captures when the user selects something from its drop down list and the other captures when the user manually types or pastes some value in. I then have both callback methods call and use the same function to process the selected item and then give some feedback to the user in the TextBox control:

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

        private void Form1_Load(object sender, EventArgs e)
        {
            // add items to the combo box
            comboBox1.Items.AddRange(new object[] {
            "Cat",
            "Dog",
            "Bird",
            "Fish"});
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            // the user selected a drop down value so process it
            processSelection();
        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            // the user manually entered a value so process it
            processSelection();
        }

        // custom function to process the combobox text
        private void processSelection()
        {
            // get the current text
            string tempText = comboBox1.Text.Trim().ToLower();
            
            // check if it is one of the items
            if (tempText == "")
            {
                textBox1.Text = "";
            }
            else if (tempText == "cat")
            {
                textBox1.Text = "Cats are small carnivorous mammals. The scientific name for Cat is Felis catus.";
            }
            else if (tempText == "dog")
            {
                textBox1.Text = "Dogs are descendants of the wolf.";
            }
            else if (tempText == "bird")
            {
                textBox1.Text = "There are over 10,000 different species of birds around the world.";
            }
            else if (tempText == "fish")
            {
                textBox1.Text = "The whale shark is the largest fish, measuring over 18 meters in length.";
            }
            else
            {
                textBox1.Text = "Your favorite pet is a " + comboBox1.Text;
            }
        }
    }
}

When you run the above examples and either select something from the ComboBox's drop down list or type something into the ComboBox's text box then you should see something similar to the following:

example3
example4
example5
example6

Final Thoughts

Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.


(0) pollYesResult
(0) pollNoResult



 
     About   |   Contact Us   |   Privacy   |   Terms & Conditions   |   © 2024 - T&J Divisions, LLC, All Rights Reserved