C# – All in One → C# Windows Forms ComboBox CRUD – ADD UPDATE DELETE – Camposha

C# – All in One

C# – All in One

C# – All in One

C# Windows Forms ComboBox CRUD – ADD UPDATE DELETE

In this class we want t see how to add, update, delete and clear items to and from a ComboBox in windows forms.
This is a CRUD tutorial to help us in understanding how to work with ComboBox.

We are Building a Vibrant YouTube Community

We have a fast rising YouTube Channel of friends. So far we’ve accumulated more than 2.6 million agreggate views and more than 10,000 subscribeers. Here’s the Channel: ProgrammingWizards TV.

Please go ahead subscribe(free obviously) as well. If you have a question or a comment you can post there instead of in this site.People are suggesting us tutorials to do there so you can too.

Here’s this tutorial in Video Format.

What is a ComboBox?

ComboBox is a Windows Forms control used to display data in a drop-down combo box.

Normally it appears in two parts:

  1. A top part which is a text box that allows the user to type a list item.
  2. The second part is a list box that displays a list of items from which the user can select one.

Selecting an Item in a ComboBox

Normally we want to get an item that is selected from a ComboBox.
We can use the SelectedIndex property which does return an integer value that corresponds to the selected list item.

If no item is selected, the SelectedIndex value is -1.

Performing CRUD in ComboBox

To add or delete items in a ComboBox control, use the Add, Insert, Clear or Remove method.

This is what we do in this class, performing CRUD on a ComboBox.

Let’s go.

Program.cs

This is our only class. We are creating everything from scratch as opposed to using the designer so that we can understand how ComboBox and windows froms in general work.

using System;
using System.Drawing;
using System.Windows.Forms;
namespace WinFormsComboBoxCRUD
{
static partial class Program
{
/*
* Instance Fields
*/
private static ComboBox myComboBox;
private static TextBox nameTxt;
private static Button addBtn;
private static Button updateBtn;
private static Button deleteBtn;
private static Button clearBtn;
private static Form myForm;
private static bool selected;
/*
* Main method
*/
public static void Main()
{
createForm();
}
/*
* Create Form
*/
private static void createForm()
{
myForm = new Form
{
Text = "C# WinForms ComboBox : CRUD - ADD UPDATE DELETE",
ClientSize = new Size(734, 484),
BackColor = Color.Coral
};
createComponents();
setUpEventHandlers();
Application.EnableVisualStyles();
Application.Run(myForm);
}
/*
* Clear textboxes
*/
private static void clearTxts()
{
nameTxt.Text = "";
}
/*
* Set up event handlers
*/
private static void setUpEventHandlers()
{
addBtn.Click += addBtn_Click;
updateBtn.Click += updateBtn_Click;
deleteBtn.Click += deleteBtn_Click;
clearBtn.Click += clearBtn_Click;
myComboBox.SelectedIndexChanged += myComboBox_SelectedIndexChanged;
}
/*
* When Add Button is clicked, add TextBoxes contents as ComboBox item
*/
static void addBtn_Click(object sender, EventArgs e)
{
myComboBox.Items.Add(nameTxt.Text);
myComboBox.SelectedItem = nameTxt.Text;
clearTxts();
}
/*
* When Update Button is clicked, update selected ComboBox item with TextBoxes contents
*/
static void updateBtn_Click(object sender, EventArgs e)
{
if (selected)
{
int selectedIndex = myComboBox.SelectedIndex;
myComboBox.Items.RemoveAt(selectedIndex);
myComboBox.Items.Insert(selectedIndex, nameTxt.Text);
}
}
/*
* When Delete Button is clicked, delete selected ComboBox item
*/
static void deleteBtn_Click(object sender, EventArgs e)
{
if (selected)
{
int selectedIndex = myComboBox.SelectedIndex;
myComboBox.Items.RemoveAt(selectedIndex);
clearTxts();
}
}
/*
* When Clear Button is clicked, clear  ComboBox and TextBoxes
*/
static void clearBtn_Click(object sender, EventArgs e)
{
myComboBox.Items.Clear();
clearTxts();
}
/*
* When ComboBoxItem is selected set selected items to TextBoxes
*/
static void myComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
nameTxt.Text = myComboBox.SelectedItem.ToString();
selected = true;
}
}
/*
* Our Partial Program class
* Exists in the same namespace.
*/
static partial class Program
{
public static void createComponents()
{
/*
* Create Labels
*/
Label nameLabel = new Label
{
Location = new Point(431, 137),
Size = new Size(45, 19),
Text = "Name"
};
/*
* Create TextBoxes
*/
nameTxt = new TextBox
{
Location = new Point(517, 137),
Size = new Size(194, 23),
TabIndex = 0
};
/*
* Create Buttons
*/
addBtn = new Button
{
Location = new Point(431, 284),
Size = new Size(83, 31),
Text = "ADD",
TabIndex = 2
};
updateBtn = new Button
{
Location = new Point(587, 284),
Size = new Size(83, 31),
Text = "UPDATE",
TabIndex = 3
};
deleteBtn = new Button { Location = new Point(431, 361), Size = new Size(83, 31), Text = "DELETE", TabIndex = 4 };
clearBtn = new Button
{
Location = new Point(587, 361),
Size = new Size(83, 31),
Text = "CLEAR",
TabIndex = 5
};
/*
* Create ComboBox
*/
myComboBox = new ComboBox { Location = new Point(24, 90), Size = new Size(400, 400),DropDownStyle = ComboBoxStyle.DropDownList};
/*
* Add Metro Components to Form
*/
myForm.Controls.Add(clearBtn);
myForm.Controls.Add(deleteBtn);
myForm.Controls.Add(updateBtn);
myForm.Controls.Add(addBtn);
myForm.Controls.Add(nameTxt);
myForm.Controls.Add(nameLabel);
myForm.Controls.Add(myComboBox);
}
}
}

Result

Explanations

Add Imports via the Using Directives

First we add our imports via the using directive:

using System;
using System.Drawing;
using System.Windows.Forms;

Define Class Data Members

Our class is a static class so we will add data members for our class. These are basically the windows forms components we will use like buttons, textbox and combobox.

        private static ComboBox myComboBox;
private static TextBox nameTxt;
private static Button addBtn;
private static Button updateBtn;
private static Button deleteBtn;
private static Button clearBtn;
private static Form myForm;
private static bool selected;   

Add Items To ComboBox

When our addBtn is clicked we will add items to our ComboBox. All we have to do is retrieve the Items property and invoke it’s Add() method:

    static void addBtn_Click(object sender, EventArgs e)
{
myComboBox.Items.Add(nameTxt.Text);
myComboBox.SelectedItem = nameTxt.Text;
clearTxts();
}

Update the Selected Item

When the update button is clicked, we will update the currently selected item. Updating in this sense basically implies removing the current item and adding a new one.

        static void updateBtn_Click(object sender, EventArgs e)
{
if (selected)
{
int selectedIndex = myComboBox.SelectedIndex;
myComboBox.Items.RemoveAt(selectedIndex);
myComboBox.Items.Insert(selectedIndex, nameTxt.Text);
}
}

Delete the Selected ComboBox Item

When the user clicks delete button, we will delete the selected item.

This basically means we invoke the RemoveAt() method of our combobox, passing in the index, in this case the selected index.

        static void deleteBtn_Click(object sender, EventArgs e)
{
if (selected)
{
int selectedIndex = myComboBox.SelectedIndex;
myComboBox.Items.RemoveAt(selectedIndex);
clearTxts();
}
}

Remove All ComboBox Items

To remove all combobox items we use the Clear() method of the ComboBox class. This will remove everything. We do this when the ClearBtn is clicked.

        static void myComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
nameTxt.Text = myComboBox.SelectedItem.ToString();
selected = true;
}

Best Regards.

Leave a Reply

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

Join Us
X