Panel

This acts as container, which can contain other type of controls like labels, textboxes, buttons, checkboxes etc.
panel Exalple

API: System.Windows.Forms.Panel
Naming Convension: pnlxxxxxx
Properties of Panel
Property Description
Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

Demo on Panel
panel Exalple

Design
panel1
button1
button2

private void panel1_Click(object sender, EventArgs e)
{
MessageBox.Show("You have clicked the panel");
}

Note: When you want to create multiple groups of radio buttons, then use the panel or group box control to group-up those radio buttons.

GroupBox

This is also acts as container, similar to panel, but it contains text also.
panel Exalple

API: System.Windows.Forms.GroupBox
Naming Convension: grpxxxxxx
Properties of GroupBox
Property Description
Name, Text, BackColor, ForeColor, Font, Enabled, Visible, Size, Location, BackgroundImage, BackgroundImageLayout, ContextMenuStrip

Events of GroupBox
Property Description
Enter, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Leave

Demo on GroupBox
panel Exalple

Design
txtMyTextBox
grpBackgroundColor
grpForegroundColor
rbWhite
rbRed
rbYellow
rbBlue
rbGreen
rbOrange

private void rbWhite_CheckedChanged(object sender, EventArgs e)
{
txtMyTextBox.BackColor = Color.White;
}

private void rbRed_CheckedChanged(object sender, EventArgs e)
{
txtMyTextBox.BackColor = Color.Red;
}
private void rbYellow_CheckedChanged(object sender, EventArgs e)
{
txtMyTextBox.BackColor = Color.Yellow;
}

private void rbBlue_CheckedChanged(object sender, EventArgs e)
{
txtMyTextBox.ForeColor = Color.Blue;
}

private void rbGreen_CheckedChanged(object sender, EventArgs e)
{
txtMyTextBox.ForeColor = Color.Green;
}

private void rbOrange_CheckedChanged(object sender, EventArgs e)
{
txtMyTextBox.ForeColor = Color.Orange;
}

ListBox

This contains multiple options (items). Among those items, the user can select any one option. In some list boxes, multiple items also can be selected. Those list boxes are called as “Mutiple item selection list boxes”.
panel Exalple
API: System.Windows.Forms.ListBox
Naming Convension: lstxxxxxx
Properties of ListBox
Property Description
Items Contains the list of items, that can be displayed in the list box.
SelectionMode Specifies mode of the item selection.
None – No item can be selected.
One - Single item can only be selected
MultiSimple – Multiple items can be selected, directly by clicking on the items.
MultiExtended – Multiple items can be selected, with Shift+Click or Ctrl+Click.
Sorted Enables / disables automatic sorting of items
Name, BackColor, ForeColor, Font, Enabled, Visible, Size, Location, ContextMenuStrip

Events of ListBox
Event Description
SelectedIndexChanged, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Leave

Run Time Properties of ListBox
Property Description
lstObj.SelectedItem Represents the currently selected item in the list box.
lstObj.SelectedIndex Represents the index of the currently selected item in the list box.
lstObj.Items.Count Represents the total no. of items in the list box.
lstObj.Items[index] Gets the specified item, based on the given index.
lstObj.SelectedItems.Count Represents the total no. of items, currently being selected. (For multiple selection list boxes)
lstObj.SelectedItems[index] Gets the particular item in the currently selected items. (For multiple selection list boxes)

Methods of ListBox

Property Description
lstObj.Items.Add(“xxxxx”) Adds a new item at the end of the list box items.
lstObj.Items.Insert(index, “xxxx”) Inserts a new item at the specified position.
lstObj.Items.RemoveAt(index) Removes an item, based on its index.
lstObj.Items.Clear() Removes all the items in the list box.
lstObj.Items.IndexOf(“xxxx”) Searches the given string the items collection, and returns the index, if it found; otherwise, it returns -1.

Demo on Single item Selection ListBox
panel Exalple

Design

lblSelectCourse
lstCourses
lblSelectedCoursePrompt
lblSelectedCourse
lblSelectedCourseIndexPrompt
lblSelectedCourseIndex


private void lstCourses_SelectedIndexChanged(object sender, EventArgs e)
{
lblSelectedCourse.Text = Convert.ToString(lstCourses.SelectedItem);
lblSelectedCourseIndex.Text = Convert.ToString(lstCourses.SelectedIndex);
}

Demo on Single item Selection ListBox

panel Exalple
Design
grpCities
lstCities
grpOptions
lblNewCity
txtNewCity
btnAdd
btnRemoveCity
brnClearAll
btnShowCount


private void btnAdd_Click(object sender, EventArgs e)
{
if (txtNewCity.Text != "")
{
lstCities.Items.Add(txtNewCity.Text);
txtNewCity.Clear();
}
else
MessageBox.Show("Enter new city name.");
txtNewCity.Focus();
}

private void btnRemoveCity_Click(object sender, EventArgs e)
{
if (lstCities.SelectedIndex >= 0)
lstCities.Items.RemoveAt(lstCities.SelectedIndex);
else
MessageBox.Show("Select any city.");
}

private void btnClearAll_Click(object sender, EventArgs e)
{
lstCities.Items.Clear();
MessageBox.Show("All cities cleared.");
}

private void btnShowCount_Click(object sender, EventArgs e)
{
int count = lstCities.Items.Count;
MessageBox.Show(count + " cities found.");
}

Demo on Multiple item Selection ListBox

panel Exalple
Design
lblAvailableBooks
lblSelectedBooks
lstAvailableBooks
Sorted: true
SelectionMode: MultiSimple
lstSelectedBooks
Sorted: true
btnSend


private void btnSend_Click(object sender, EventArgs e)
{
int i;
lstSelectedBooks.Items.Clear();
for (i = 0; i < lstAvailableBooks.SelectedItems.Count; i++)
lstSelectedBooks.Items.Add(lstAvailableBooks.SelectedItems[i]);
}

ComboBox

This also contains multiple options (items), similar to list box. But, unlike list box, in the combo box, the user can’t select multiple items. One more advantage of combo box is, it offers some text entry similar to text box. Finally a combo box, is combination of list box and text box.
panel Exalple
API: System.Windows.Forms.ComboBox
Naming Convension: cmbxxxxxx
Properties of ComboBox
Property Description
Items Contains the list of items, that can be displayed in the list box.
DropDownStyle Simple: It looks like a text box, but the items can be accessed by pressing up / down arrow keys.
DropDown: It is the default value. The user can type new text (or) can select the items from the list.
DropDownList: The user can type new text. Only selection is possible.
Sorted Enables / disables automatic sorting of items
Name, Text, TextAlign, BackColor, ForeColor, Font, Enabled, Visible, Size, Location, ContextMenuStrip

Events of ComboBox
Event Description
SelectedIndexChanged, TextChanged, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Leave

Run Time Properties of Combo Box
Property Description
cmbObj.SelectedItem Represents the currently selected item in the combo box.
cmbObj.SelectedIndex Represents the index of the currently selected item in the combo box.
cmbObj.Items.Count Represents the total no. of items in the combo box.
cmbObj.Items[index] Gets the specified item, based on the given index.
cmbObj.Text Gets the text, entered in the combo box.

Methods of ComboBox
Method Description
cmbObj.SelectedItem Represents the currently selected item in the combo box.
cmbObj.SelectedIndex Represents the index of the currently selected item in the combo box.
cmbObj.Items.Count Represents the total no. of items in the combo box.
cmbObj.Items[index] Gets the specified item, based on the given index.
cmbObj.Text Gets the text, entered in the combo box.

Method Description
cmbObj.Items.Add(“xxxxx”) Adds a new item at the end of the combo box items.
cmbObj.Items.Insert(index, “xxxx”) Inserts a new item at the specified position.
cmbObj.Items.RemoveAt(index) Removes an item, based on its index.
cmbObj.Items.Clear() Removes all the items in the combo box.
cmbObj.Items.IndexOf(“xxxx”) Searches the given string the items collection, and returns the index, if it found; otherwise, it returns -1.
cmbObj.Clear() Clears the text entered in the combo box.

Demo on ComboBox

panel Exalple
Design
lblSelectCourse
cmbCourse
Items: .NET, Java, C, C++, Oracle
DropDownStyle: DropDownList
lblFee
txtFee
ReadOnly: true


private void cmbCourse_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbCourse.SelectedIndex == 0)
txtFee.Text = "Rs. 3,500/-";
else if (cmbCourse.SelectedIndex == 1)
txtFee.Text = "Rs. 6,200/-";
else if (cmbCourse.SelectedIndex == 2)
txtFee.Text = "Rs. 1,000/-";
else if (cmbCourse.SelectedIndex == 3)
txtFee.Text = "Rs. 1,200/-";
else if (cmbCourse.SelectedIndex == 4)
txtFee.Text = "Rs. 1,500/-";
}