Theory C#
Theory C#
Label Control
Labels are one of the most frequently used C# control. We can use the Label control to
display text in a set location on the page. Label controls can also be used to add
descriptive text to a Form to provide the user with helpful information.
Drag label from the toolbox and Change the label name to label control text property
Button Control
Windows Forms controls are reusable components that encapsulate user interface
functionality and are used in client side Windows applications. A button is a control,
which is an interactive component that enables users to communicate with an
application. The Button class inherits directly from the ButtonBase class. A Button can
be clicked by using the mouse, ENTER key, or SPACEBAR if the button has focus.
When you want to change display text of the Button , you can change the Text property
of the button.
TextBox Control
A TextBox control is used to display, or accept as input, a single line of text. This control
has additional functionality that is not found in the standard Windows text box control,
including multiline editing and password character masking.
A text box object is used to display text on a form or to get user input while a C#
program is running. In a text box, a user can type data or paste it into the control from
the clipboard.
ComboBox Control
C# controls are located in the Toolbox of the development environment, and you use
them to create objects on a form with a simple series of mouse clicks and dragging
motions. A ComboBox displays a text box combined with a ListBox, which enables the
user to select items from the list or enter a new value.
The user can type a value in the text field or click the button to display a drop down list.
You can add individual objects with the Add method. You can delete items with the
Remove method or clear the entire list with the Clear method.
If you want to retrieve the displayed item to a string variable , you can code like this
You can remove items from a combobox in two ways. You can remove item at a the
specified index or giving a specified item by name.
The above code will remove the second item from the combobox.
The above code will remove the item "Friday" from the combobox.
DropDownStyle
The DropDownStyle property specifies whether the list is always displayed or whether
the list is displayed in a drop-down. The DropDownStyle property also specifies whether
the text portion can be edited.
ListBox Control
The ListBox control enables you to display a list of items to the user that the user can
select by clicking.
You can set ListBox properties by using Properties Window. In order to get Properties
window you can Press F4 or by right-clicking on a control to get the "Properties" menu
item.
In this method you add item in the listbox by edit the item collection this process is to
add items in the listbox at design time.
If you want to retrieve a single selected item to a variable , use the following code.
OR
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
The SelectionMode property determines how many items in the list can be selected at
a time. A ListBox control can provide single or multiple selections using the
SelectionMode property . If you change the selection mode property to multiple select ,
then you will retrieve a collection of items from ListBox1.SelectedItems property.
In Multiple mode, you can select or deselect any item in a ListBox by clicking it. In
Extended mode, you need to hold down the Ctrl key to select additional items or the
Shift key to select a range of items.
The following C# program initially fill seven days in a week while in the form load event
and set the selection mode property to MultiSimple . At the Button click event it will
display the selected items.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ListBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
MessageBox.Show(items,"Listbox",MessageBoxButtons.OK,MessageBoxIcon.Informati
on);
}
}
}
RadioButton Control
A radio button or option button enables the user to select a single option from a group of
choices when paired with other RadioButton controls. When a user clicks on a radio
button, it becomes checked, and all other radio buttons with same group become
unchecked.
Program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
CheckBox Control
CheckBoxes allow the user to make multiple selections from a number of options.
CheckBox to give the user an option, such as true/false or yes/no. You can click a
check box to select it and click it again to deselect it.
The CheckBox control can display an image or text or both. Usually CheckBox comes
with a caption, which you can set in the Text property.
The radio button and the check box are used for different functions. Use a radio button
when you want the user to choose only one option. When you want the user to choose
all appropriate options, use a check box. The following C# program shows how to find a
checkbox is selected or not.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
if (checkBox1.Checked == true)
{
msg = "Black\n";
}
if (checkBox2.Checked == true)
{
msg = msg +"Blue\n";
}
if (checkBox3.Checked == true)
{
msg = msg + "Green";
}
if (msg.Length > 0)
{
MessageBox.Show(msg,"Message");
}
else
{
MessageBox.Show("No checkbox is selected","Message");
}
}
}
}