Database Reference
In-Depth Information
Figure 9-18.
Using CheckBox and RadioButton with UserInfo Windows Forms application
How It Works
Let's take a look at the “Adding a New Form to the Windows Project” task first and understand the code,
button by button and line by line.
First you have an Add button, which adds the country and state to the ListBox and ComboBox. The
ListBox and ComboBox controls have a collection named
Items
, and this collection can contain a list of
items, which is why you use it here. Next you call up the
Add
method of the
Items
collection, and finally
you pass the value entered in the TextBox to the ListBox's or ComboBox's
Items
collection's
Add
method,
as shown here:
lstCountry.Items.Add(txtCountry.Text);
Also, once an item is added, for a better user experience, it is advisable to clear the text box so a user
can type a new value with ease.
txtCountry.Clear();
You repeat the same for the ComboBox:
cboState.Items.Add(txtState.Text);
txtState.Clear();
For the Remove Country and Remove State buttons, you follow a similar approach as the
Items
collection, but this time instead of
Add()
you call the
Remove()
method, and as you know, the
prerequisites for an item removal is that and item has to be selected first either in the ListBox or the
ComboBox. Hence, the code seeks a
SelectedItem
to be passed to the
Remove()
method.