Article

Add items in combobox winforms c#

In this article we will learn add items in combobox winforms c#.

In this blog we will see step by step add items in combobox winforms c#

Step 1. We will add one ComboBox, one textbox, one label and one button on windows form.

Step 2. In this step we will change the text of label and button. Need to open properties of label and button for change text and Name just like given below screenshot.

Step 3. In this step we will open properties of ComboBox and select AutoCompleteMode in SuggestAppend and AutoCompleteSource in ListItems

Notes
AutoCompleteMode Specifies the mode for the automatic completion feature used in the ComboBox.
AutoCompleteSource Specifies the source for ComboBox and TextBox automatic completion functionality.
ListItems Specifies that the items of the ComboBox represent the source.
SuggestAppend is generally Applies both Suggest and Append options.

Step 4. In this step we will double click on Add button for generate click event. After double click Form1.cs page will open.

Step 4. Write this code in Form1.cs page for add items in combobox winforms c#

Notes
comboBox1 is the name of listbox
textBox1 is the name of textbox
item: An object representing the item to add to the collection.
Add : Add an items in listbox

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

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

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                comboBox1.Items.Add(textBox1.Text);
                textBox1.Clear();
                MessageBox.Show("Item is added in ComboBox");
                textBox1.Focus();
            }
            else
            {
                MessageBox.Show("Please enter item in TextBox");
            }
        }
    }
}



Output

Leave a Reply

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