0% found this document useful (0 votes)
5 views33 pages

WINDOWSFORMS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views33 pages

WINDOWSFORMS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Controls in Windows Applications

I. Core Controls
1) Button
2) Label
3) Link Label
4) TextBox
5) PictureBox
6) Panel
7) GroupBox
8) CheckBox
9) RadioButton
10) ListBox
11) ComboBox
12) DomainUpDown
13) NumericUpDown
14) DateTimePicker
15) MonthCalendar
II. Background Process Controls
16) Timer
17) ProgressBar
III. Menu Controls
18) MenuStrip
19) ContextMenuStrip
IV. Dialog Box Controls
20) ColorDialog
21) FontDialog
22) FolderBrowserDialog
23) OpenFileDialog
24) SaveFileDialog
25) PrintDialog
V. Database Controls
26) DataGridView
VI. System Tray Controls
27) NotifyIcon
VII. Other User Friendly Controls
28) TabControl
29) RichTextBox
30) TrackBar
31) TreeView

1) Button

Executes an operation, when it is clicked.


API: System.Windows.Forms.Button
Naming Convension: btnxxxxxx

Properties of Button
Property Description
Name Specifies the name of the control.
Text Specifies the displayable text of the control.
BackColor Specifies the background color of the control.
ForeColor Specifies the foreground color of the control.
Font Specifies the font style of the control’s text.
Enabled Enables / Disables the control.
Visible Displays / Hides the control.
Cursor Specifies the mouse pointer style, when it is over on the control.
Size Specifies the Width and Height of the control.
Location Specifies the X and Y co-ordinations of the control’s position on the form.
TextAlign Specifies the position of the text in the control.
Specifies the image that is to be displayed in the control along with the
Image
text.
ImageAlign Specifies the position of the image in the control
TabIndex Specifies the index of the control in the tab order.
ContextMenuStrip Contains the reference of the respective context menu control.

Events of Button
Event Description
Click Executes when the user clicks the control run time.
DoubleClick Executes when the user double-clicks the control at run time.
MouseMove Executes when the mouse pointer is moves across the control.
MouseEnter Executes when the mouse pointer is focused on to the control.
MouseLeave Executes when the mouse pointer is out of the control.
Executes when any key is pressed on the keyboard, while the focus is on
KeyPress
the control.
Enter Executes when the focus is got on to the control.
Leave Executes when the focus is out of the control.

Application 67: Demo on Buttons

Design

button1:
Text: Red
Name: btnRed

button2:
Text: Green
Name: btnGreen

button3:
Text: Blue
Name: btnBlue

private void btnRed_Click(object sender, EventArgs e)


{
this.BackColor = Color.Red;
}

private void btnGreen_Click(object sender, EventArgs e)


{
this.BackColor = Color.Green;
}

private void btnBlue_Click(object sender, EventArgs e)


{
this.BackColor = Color.Blue;
}

Application 68: Demo on Buttons


Design

button1:
Text: Normal
Name: btnNormal

button2:
Text: Minimize
Name: btnMinimize

button3:
Text: Maximize
Name: btnMaximize

button4:
Text: Exit
private void btnNormal_Click(object sender, EventArgs e)
Name: btnExit
{
this.WindowState = FormWindowState.Normal;
}

private void btnMinimize_Click(object sender, EventArgs e)


{
this.WindowState = FormWindowState.Minimized;
}

private void btnMaximize_Click(object sender, EventArgs e)


{
this.WindowState = FormWindowState.Maximized;
}

private void btnExit_Click(object sender, EventArgs e)


{
this.Close();
}

Application 69: Demo on Buttons

button1:
Design
Text: Show Background Image
Name: btnShowBackgroundImage

button2:
Text: Clear Background Image
Name: btnClearBackgroundImage

private void btnShowBackgroundImage_Click(object sender, EventArgs e)


{
this.BackgroundImage = Image.FromFile("c:\\globe.jpg");
this.BackgroundImageLayout = ImageLayout.Zoom;
}

private void btnClearBackgroundImage_Click(object sender, EventArgs e)


{
2) Label

Mainly used for presentation purpose, to display a message or description to the user.

API: System.Windows.Forms.Label
Naming Convension: lblxxxxxx

Properties of Label
Property Description
Name Specifies the name of the control.
Text Specifies the displayable text of the control.
BackColor Specifies the background color of the control.
ForeColor Specifies the foreground color of the control.
Font Specifies the font style of the control’s text.
Enabled Enables / Disables the control.
Visible Displays / Hides the control.
Cursor Specifies the mouse pointer style, when it is over on the control.
Size Specifies the Width and Height of the control.
Specifies the X and Y co-ordinations of the control’s position on the
Location
form.
TextAlign Specifies the position of the text in the control.
Specifies the image that is to be displayed in the control along with
Image
the text.
ImageAlign Specifies the position of the image in the control
TabIndex Specifies the index of the control in the tab order.
ContextMenuStrip Contains the reference of the respective context menu control.
Enables / disables automatic sizing of the control, based on the
AutoSize
text.

Events of Label
Event Description
Click Executes when the user clicks the control run time.
DoubleClick Executes when the user double-clicks the control at run time.
MouseMove Executes when the mouse pointer is moves across the control.
MouseEnter Executes when the mouse pointer is focused on to the control.
MouseLeave Executes when the mouse pointer is out of the control.
Executes when any key is pressed on the keyboard, while the focus
KeyPress
is on the control.
Enter Executes when the focus is got on to the control.
Leave Executes when the focus is out of the control.

Application 70: Demo on Label


Design
label1:
Text: .NET Framework
Name: lblMessage

private void Form1_Load(object sender, EventArgs e)


{
lblMessage.BackColor = Color.Yellow;
lblMessage.ForeColor = Color.Green;
}
private void lblMessage_MouseEnter(object sender, EventArgs e)
{
lblMessage.BackColor = Color.Green;
lblMessage.ForeColor = Color.Yellow;
}

private void lblMessage_MouseLeave(object sender, EventArgs e)


{
lblMessage.BackColor = Color.Yellow;
lblMessage.ForeColor = Color.Green;
}

3) TextBox
Used to take any user input in the application.
API: System.Windows.Forms.TextBox
Naming Convension: txtxxxxxx

Properties of TextBox
Property Description
Name Specifies the name of the control.
Text Specifies the displayable text of the control.
BackColor Specifies the background color of the control.
ForeColor Specifies the foreground color of the control.
Font Specifies the font style of the control’s text.
Enabled Enables / Disables the control.
Visible Displays / Hides the control.
Cursor Specifies the mouse pointer style, when it is over on the control.
Size Specifies the Width and Height of the control.
Specifies the X and Y co-ordinations of the control’s position on the
Location
form.
TextAlign Specifies the position of the text in the control.
Specifies the image that is to be displayed in the control along with
Image
the text.
ImageAlign Specifies the position of the image in the control
TabIndex Specifies the index of the control in the tab order.
ContextMenuStrip Contains the reference of the respective context menu control.
ReadOnly Enables / disables read-only nature of the textbox. In the read only
textbox, the user can not enter any text.
Enables / disables multiple lines in the text box. By default, the text
MultiLine
box will be single-line textbox.
This is used in multi line textboxes, which automatically moves the
WordWrap
cursor to the next line, when the current line exceeds.
Scrollbars Enables / disables scroll bars in the textbox.
PasswordChar Used to specify the password display character. Ex: *
Specifies the maximum no. of characters, that can be entered in
MaxLength
the textbox.
Events of TextBox
Event Description
TextChanged Executes when any character is typed / removed in the textbox.
Click Executes when the user clicks the control run time.
DoubleClick Executes when the user double-clicks the control at run time.
MouseMove Executes when the mouse pointer is moves across the control.
MouseEnter Executes when the mouse pointer is focused on to the control.
MouseLeave Executes when the mouse pointer is out of the button.
Executes when any key is pressed on the keyboard, while the focus
KeyPress
is on the control.
Enter Executes when the focus is got on to the control.
Leave Executes when the focus is out of the control.
Methods of TextBox
Method Description
Clear() Clears all the contents of the textbox and makes it empty.
Focus() Moves the focus to the control.
label1: Design
Application 71: Demo on TextBox Text: Enter your Name:
Name: lblName label1:
Text: Enter your Name:
textBox1:
Name: lblName
Name: txtName
label2: textBox1:
Text: Message Name: txtName
Name: lblMessage
Visible: false label2:
Text: Message
button1:
Name: lblMessage
Name: btnOK
Visible: false
Text: OK
ForeColor: Red
button1:
Name: btnOK
private void btnOK_Click(object sender, EventArgs e)
Text: OK
{
ForeColor: Red
string name = txtName.Text;
string message = "Welcome to " + name;
lblMessage.Text = message;
lblMessage.Visible = true;
}

Design
Application 72: Demo on TextBox
label1:
Text: Enter First Value:
Name: lblFirstValue

textBox1:
Name: txtFirstValue

label2:
Text: Enter Second Value:
Name: lblSecondValue

textBox2:
Name: txtSecondValue

button1:
Name: btnAdd
Text: +

button2:
Name: btnSubtract
private void btnAdd_Click(object sender, EventArgs e) Text: -
{
int a = Convert.ToInt32(txtFirstValue.Text); button3:
int b = Convert.ToInt32(txtSecondValue.Text); Name: btnMultiply
int c = a + b; Text: *
txtResult.Text = Convert.ToString(c);
} button4:
Name: btnDivide
private void btnSubtract_Click(object sender, EventArgs e) Text: /
{
int a = Convert.ToInt32(txtFirstValue.Text); label3:
int b = Convert.ToInt32(txtSecondValue.Text); Text: Result:
int c = a - b; Name: lblResult
txtResult.Text = Convert.ToString(c);
} textBox3:
Name: txtResult
ReadOnly: true

private void btnMultiply_Click(object sender, EventArgs e)


{
int a = Convert.ToInt32(txtFirstValue.Text);
int b = Convert.ToInt32(txtSecondValue.Text);
int c = a * b;
txtResult.Text = Convert.ToString(c);
}
private void btnDivide_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtFirstValue.Text);
int b = Convert.ToInt32(txtSecondValue.Text);
int c = a / b;
txtResult.Text = Convert.ToString(c);
}
Application 73: Demo on TextBox
Design
label1:
Text: Enter your text here:
Name: lblSourceText

textBox1:
Name: txtSourceText

label2:
Text: Copied Text:
Name: lblDestinationText

textBox2:
Name: txtDestinationText

private void txtSourceText_TextChanged(object sender, EventArgs e)


{
txtDestinationText.Text = txtSourceText.Text;
}

Application 74: Demo on TextBox


Design
label1:
Text: Generate Numbers
Name: lblGenerateNumbers

label2:
Text: From:
Name: lblFrom

textBox1:
Name: txtFrom

label3:
Text: To:
Name: lblTo

textBox2:
Name: txtTo

button1:
Text: GO:
Name: btnGO

private void btnGO_Click(object sender, EventArgs e) textBox3:


{ Name: txtNumbers
int n1 = Convert.ToInt32(txtFrom.Text); Multiline: true
int n2 = Convert.ToInt32(txtTo.Text); Readonly: true
txtNumbers.Clear(); Scrollbars: Vertical
for (int i = n1; i <= n2; i++)
{
txtNumbers.Text = txtNumbers.Text + i + ", ";
}
txtFrom.Focus();
}

4) CheckBox

Used to take the choice from the user. The check box can be checked or un-
checked by the user.

API: System.Windows.Forms.CheckBox
Naming Convension: chkxxxxxx
Properties of CheckBox
Property Description
Name Specifies the name of the control.
Represents the current status of the check box, whether it is
Checked
checked or un-checked.
Text Specifies the displayable text of the control.
BackColor, ForeColor, Font, Enabled, Visible, Cursor, Size, Location, TextAlign, Image, ImageAlign,
TabIndex, ContextMenuStrip
Events of CheckBox
Event Description
CheckedChanged Executes when the user checks / un-checks the checkbox.
Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

Methods of CheckBox
Method Description
Focus() Moves the focus to the control.

Application 75: Demo on CheckBox

Design
checkBox1
label1

private void checkBox1_CheckedChanged(object sender, EventArgs e)


{
if (checkBox1.Checked == true)
label1.Text = "The check box is checked.";
else
label1.Text = "The check box is un-checked.";
}

5) RadioButton

Used to take the choice from the user. We have to implement two or
more radio buttons. At run time, any one of the radio buttons can be
selected.

API: System.Windows.Forms.RadioButton
Naming Convension: rbxxxxxx
Properties of RadioButton
Property Description
Name Specifies the name of the control.
Text Specifies the displayable text of the control.
Represents the current status of the check box, whether it is
Checked
checked or un-checked.
BackColor, ForeColor, Font, Enabled, Visible, Cursor, Size, Location, TextAlign, Image, ImageAlign,
TabIndex, ContextMenuStrip
Events of RadioButton
Event Description
CheckedChanged Executes when the user selects the radio button.
Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave
Methods of RadioButton
Method Description
Focus() Moves the focus to the control.

Application 76: Demo on Radio Button Design


label1:
Text: Select Background Color:
Name: lblBackgroundColor

radioButton1:
Text: Beige
Name: rbBeige

radioButton2:
Text: Light Green
Name: rbLightGreen

radioButton3:
Text: Light Yellow
Name: rbLightYellow
radioButton4:
Text: Bisque:
Name: rbBisque

private void rbBeige_CheckedChanged(object sender, EventArgs e)


{
this.BackColor = Color.Beige;
}

private void rbLightGreen_CheckedChanged(object sender, EventArgs e)


{
this.BackColor = Color.LightGreen;
}

private void rbLightYellow_CheckedChanged(object sender, EventArgs e)


{
this.BackColor = Color.LightYellow;
}

private void rbBisque_CheckedChanged(object sender, EventArgs e)


{
this.BackColor = Color.Bisque;
}
6) LinkLabel
Used to create hyperlinks.

API: System.Windows.Forms.LinkLabel
Naming Convension: lnkxxxxxx

Properties of LinkLabel
Property Description
Name Specifies the name of the control.
Text Specifies the displayable text of the control.
LinkColor Specifies the default link color
VisitedLinkColor Specifies the visited link color
ActiveLinkColor Specifies the active link color
BackColor, ForeColor, Font, Enabled, Visible, Cursor, Size, Location, TextAlign, Image, ImageAlign,
TabIndex, ContextMenuStrip
Events of LinkLabel
Event Description
LinkClicked Executes when the user clicks on the link.
Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

Methods of LinkLabel
Method Description
Focus() Moves the focus to the control.

Application 77: Demo on Link Label


Design
linkLabel1:
Text: My Link Label:
Name: linkLabel1

private void linkLabel1_LinkClicked(object


sender, LinkLabelLinkClickedEventArgs e)
{
MessageBox.Show("The link label is
clicked.");
}

7) PictureBox
Used to display an image on the form, at desired place.

API: System.Windows.Forms.PictureBox
Naming Convension: picxxxxxx

Properties of PictureBox
Property Description
Name Specifies the name of the control.
Image Specifies the image, which is to be displayed in the control.
Specifies mode of the image sizing in the control.
SizeMode
(Normal, Stretch, Auto Size, Center, Zoom)
BackColor, Enabled, Visible, Cursor, Size, Location, ContextMenuStrip
Events of PictureBox
Event Description
Click Executes when the user clicks on the picture box.
DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

Design

using System.IO; lblEnterImagePath

private void btnShow_Click(object sender, EventArgs e)


{
string imagepath = txtImagePath.Text;
FileInfo fobj = new FileInfo(imagepath);
if (fobj.Exists)
{
picBoxImage.Image = Image.FromFile(imagepath);
}
Application 78: Demo
else on PictureBox
{
MessageBox.Show("Image file not found.");
}
}
private void btnClearImage_Click(object sender, EventArgs e)
{
picBoxImage.Image = null;
}
private void rbNormal_CheckedChanged(object sender, EventArgs e)
{
picBoxImage.SizeMode = PictureBoxSizeMode.Normal;
}
private void rbStretch_CheckedChanged(object sender, EventArgs e)
{
picBoxImage.SizeMode = PictureBoxSizeMode.StretchImage;
}
private void rbAutoSize_CheckedChanged(object sender, EventArgs e)
{
picBoxImage.SizeMode = PictureBoxSizeMode.AutoSize;
}
private void rbCenter_CheckedChanged(object sender, EventArgs e)
{
picBoxImage.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void rbZoom_CheckedChanged(object sender, EventArgs e)
{
picBoxImage.SizeMode = PictureBoxSizeMode.Zoom;
}
8) Panel

This acts as container, which can contain other type of


controls like labels, textboxes, buttons, checkboxes
etc.

API: System.Windows.Forms.Panel
Naming Convension: pnlxxxxxx

Properties of Panel
Property Description
Name, BackColor, ForeColor, Font, Enabled, Visible, Size, Location, BackgroundImage,
BackgroundImageLayout, ContextMenuStrip, BorderStyle

Events of Panel
Event Description
Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

Application 79: Demo on Panel


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.

9) GroupBox

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


also.

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
Event Description
Enter, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Leave

Application 80: Demo on GroupBox


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;
}

10) 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”.

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.
Specifies mode of the item selection.
None – No item can be selected.
SelectionMode 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.
Represents the total no. of items, currently being selected. (For
lstObj.SelectedItems.Count
multiple selection list boxes)
Gets the particular item in the currently selected items. (For
lstObj.SelectedItems[index]
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.
Searches the given string the items collection, and returns the
lstObj.Items.IndexOf(“xxxx”)
index, if it found; otherwise, it returns -1.

Application 81: Demo on Single item Selection ListBox

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);
}
Application 82: Demo on Single item Selection ListBox

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();
}
Application 83: Demo on Multiple item Selection ListBox

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]);
}

11) 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.

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.
Simple: It looks like a text box, but the items can be accessed by pressing up / down
arrow keys.
DropDownStyle 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.
Represents the index of the currently selected item in the combo
cmbObj.SelectedIndex
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.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.
Searches the given string the items collection, and returns the
cmbObj.Items.IndexOf(“xxxx”)
index, if it found; otherwise, it returns -1.
cmbObj.Clear() Clears the text entered in the combo box.
lblSelectCourse
cmbCourse
Application 84: Demo on ComboBox Items: .NET, Java, C, C++,
Oracle
Design 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/-";
}

12) NumericUpDown

This offers to enter a numerical value, within a given range. The user can enter a
value, out of the range.

API: System.Windows.Forms.NumericUpDown
Naming Convension: numxxxxxx
Properties of NumericUpDown
Property Description
Value Gets or sets the current value in the NumericUpDown control.
DecimalPlaces Specifies the no. of decimal places in the value
Minimum Specifies the minimum value in the range.
Maximum Specifies the maximum value in the range.
TextAlign Left / Center / Right
UpDownAlign Left / Right
Name, ReadOnly, BackColor, ForeColor, Font, Enabled, Visible, Size, Location, BackgroundImage,
BackgroundImageLayout, ContextMenuStrip, BorderStyle

Events of NumericUpDown
Event Description
ValueChanged, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

13) DomainUpDown

It is similar to combo box, but it looks like NumericUpDown.

API: System.Windows.Forms.DomainUpDown
Naming Convension: domxxxxxx

Properties of DomainUpDown
Property
Items, Sorted, Name, Text, TextAlign, BackColor, ForeColor, Font, Enabled, Visible, Size, Location,
ContextMenuStrip
Events of DomainUpDown
Event
SelectedItemChanged, TextChanged, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave,
KeyPress, Leave
Run Time Properties of DomainUpDown
Property
domObj.SelectedItem, domObj.SelectedIndex, domObj.Items.Count, domObj.Items[index], domObj.Text
Methods of DomainUpDown
Method
domObj.Items.Add(“xxxxx”) Adds a new item at the end of the domainupdown items.
domObj.Items.Insert(index, “xxxx”) Inserts a new item at the specified position.
domObj.Items.RemoveAt(index) Removes an item, based on its index.
domObj.Items.Clear() Removes all the items in the domainupdown.
Searches the given string the items collection, and returns the
domObj.Items.IndexOf(“xxxx”)
index, if it found; otherwise, it returns -1.
domObj.Clear() Clears the text entered in the domainupdown.

Application 85: Demo on NumericUpDown and DomainUpDown

txtMyText
lblFont
lblSize
domFont
Items: Times New Roman, Tahoma,
Arial, Arial Black, Century Gothic,
Trebuchet MS, Palatino Linotype
Text: Tahoma
Design

private void ChangeFont()


{
string font = Convert.ToString(domFont.SelectedItem);
int size = Convert.ToInt32(numSize.Value);
txtMyText.Font = new Font(font, size);
}

private void domFont_SelectedItemChanged(object sender, EventArgs e)


{
ChangeFont();
}

private void numSize_ValueChanged(object sender, EventArgs e)


{
ChangeFont();
}

14) DateTimePicker

This control enables the user, to select a date or


time value at run time. When the drop down
button is clicked, it displays a calendar for date selection. When you change the “Format” property, it
offers to select the time also.

API: System.Windows.Forms.DateTimePicker
Naming Convension: dtPickerxxxxxx

Properties of DateTimePicker
Property Description
Value Gets or sets the current value in the control.
Format Specifies the format of the date selection. (Short / Long / Time / Custom)
Used to specify the customer date formats. (with words and symbols like dd,
CustomFormat
mm, yyyy, -, / etc.)
ShowUpDown Enables / Disables the “up/down” buttons in the control.
MinDate Specifies the minimum date, that can be selected at run time.
MaxDate Specifies the maximum date, that can be selected at run time.
Name, BackColor, ForeColor, Font, Enabled, Visible, Size, Location, ContextMenuStrip
Events of DateTimePicker
Event
ValueChanged, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

Application 86: Demo on DateTimePicker

Design
lblSelectDOB
dtPickerDOB
Format: Short
lblAgePrompt
lblAge

private void Form1_Load(object sender,


EventArgs e)
{
dtPickerDOB.Value =
DateTime.Now.AddYears(-20);
}
private void
dtPickerDOB_ValueChanged(object sender,
EventArgs e)
{
DateTime dob = dtPickerDOB.Value;
DateTime now = DateTime.Now;
if (now > dob)
{
TimeSpan ts = now - dob;
int Age = ts.Days / 365;
lblAge.Text = Age + " years.";
}
else
lblAge.Text = "Invalid DOB.";
}

15) MonthCalendar

Similar to DateTimePicker. It offers for a date selection. But this


control, displays the calendar directly. In the calendar, the user can
select any date.

API: System.Windows.Forms.MonthCalendar
Naming Convension: monCalxxxxxx

Properties of MonthCalendar
Property Description
ShowToday Displays / hides today’s date at the bottom of the control.
ShowWeekNumbers Displays / hides the week no’s at left side.
MinDate Specifies the minimum date, that can be selected at run time.
MaxDate Specifies the maximum date, that can be selected at run time.
Name, BackColor, ForeColor, Font, Enabled, Visible, Size, Location, ContextMenuStrip

Events of MonthCalendar
Event Description
DateChanged, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

Note: There is not any property, that gets the currently selected date in the calendar; so that, we
have to use “DateRangeEventArgs” class object to access currently selected date value in the
“DateChanged” event.

Application 86: Demo on MonthCalendar

Design

lblSelectAnyDate
monCalMyDate
ShowWeekNumbers: true
lblSelectDate

private void monCalMyDate_DateChanged(object sender, DateRangeEventArgs e)


{
lblSelectedDate.Text = "You have selected: " + e.Start.ToShortDateString();
}

16) TrackBar

Similar to numericupdown, but it offers


visualization for the value selection.

API: System.Windows.Forms.TrackBar
Naming Convension: trkxxxxxx

Properties of TrackBar
Property Description
Value Gets or sets the current value in the control.
Minimum Specifies the minimum value in the range.
Maximum Specifies the maximum value in the range.
TickFrequency Specifies the difference between each tick.
Orientation Horizontal / Vertical
TickStyle None, TopLeft, BottomRight, Both
Name, BackColor, ForeColor, Enabled, Visible, Size, Location, ContextMenuStrip, BorderStyle
Events of TrackBar
Event Description
Scroll, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

Application 88: Demo on TrackBar

Design

lblFontSize
trkFontSize
Minimum: 1
Maximum: 200
TickFrequency: 5
lblMyText
Text: 10

private void trkFontSize_Scroll(object sender, EventArgs e)


{
int n = trkFontSize.Value;
lblMyText.Font = new Font("Tahoma", n);
}

17) Timer

It is known as “Invisible Control”. That means it can’t be displayed on the form UI.
But it works during the execution time. It performs background processing. It
executes a certain logic, whenever a certain interval time is completed.

API: System.Windows.Forms.Timer
Naming Convension: tmrxxxxxx

Properties of Timer
Property Description
Interval Specifies the interval time of the timer, in the form of mille seconds.
Name, Enabled
Events of Timer
Event
Tick Executes on every completion of interval time.

Application 89: Demo on Timer


private void Form1_Load(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToLongTimeString();
}

private void tmrTime_Tick(object sender, EventArgs e)


{
lblTime.Text = DateTime.Now.ToLongTimeString();
Design }

tmrTime
Interval: 1000
Enabled: true
lblTime

Application 90: Demo on Timer

Design
tmrBackColor
Interval: 500
Enabled: true

int n = 0;

private void tmrBackColor_Tick(object sender, EventArgs e)


{
n++;
switch (n)
{
case 1: this.BackColor = Color.Black; break;
case 2: this.BackColor = Color.Blue; break;
case 3: this.BackColor = Color.LightCoral; break;
case 4: this.BackColor = Color.LightCyan; break;
case 5: this.BackColor = Color.Green; break;
case 6: this.BackColor = Color.Red; break;
case 7: this.BackColor = Color.Chocolate; break;
case 8: this.BackColor = Color.DarkKhaki; break;
case 9: this.BackColor = Color.Firebrick; break;
case 10: this.BackColor = Color.Gold; break;
default: n = 0; break;
}
}

18) ProgressBar
It shows the progress of a certain process. It’s value is
limited to the range of 0 to 100. Whenever its value is
reached to 100, that means the process is completed. It can be implemented with the combination of
timer control.

API: System.Windows.Forms.ProgressBar
Naming Convension: prgxxxxxx

Properties of ProgressBar
Property Description
Value Gets or sets the current value in the control.
Minimum Specifies the minimum value in the range.
Maximum Specifies the maximum value in the range.
Name, BackColor, ForeColor, Enabled, Visible, Size, Location, ContextMenuStrip, BackgroundImage,
BackgroundImageLayout

Events of ProgressBar
Event Description
Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

Application 91: Demo on Progress Bar Design


lblFileName
txtFileName
btnOpen
lblLoading
Visible: false
prgFile
Visible: false
txtContent
Multiline: true
Scrollbars: Both
WordWrap: false
Readonly: true
tmrFileOpen
Enabled: false
Interval: 100
using System.IO;
private void OpenFile()
{
string filename = txtFileName.Text;
FileInfo fobj = new FileInfo(filename);
if (fobj.Exists)
{
StreamReader sr = new StreamReader(filename);
string content = sr.ReadToEnd();
txtContent.Text = content;
sr.Close();
}
else
MessageBox.Show("File not found.");
}
private void btnOpen_Click(object sender, EventArgs e)
{
prgFile.Value = 0;
tmrFileOpen.Enabled = true;
lblLoading.Visible = true;
prgFile.Visible = true;
}
private void tmrFileOpen_Tick(object sender, EventArgs e)
{
prgFile.Value++;
if (prgFile.Value == 100)
{
tmrFileOpen.Enabled = false;
lblLoading.Visible = false;
prgFile.Visible = false;
OpenFile();
}
}
19) MenuStrip

Used to create a menu bar in the form. A menu bar is


a collection of multiple menu items.
items. It is known as
invisible control. The menu items are of two types.
1) Parent Menu Items
2) Child Menu Items

API: System.Windows.Forms.MenuStrip
Naming Convension: mnuxxxxxx

Each menu item will be created as a control for


“System.Windows.Forms.ToolStripMenuItem” class. The default naming convention for the
menu item is: “xxxxxToolStripMenuItem”.

Properties of MenuStrip
Property Description
Dock Top, Bottom, Left, Right, Fill
TextDirection Horizontal, Vertial90, Vertical270.
Name, BackColor, ForeColor, Font, Enabled, Visible, Size, Location, BackgroundImage,
BackgroundImageLayout, ContextMenuStrip
Events of MenuStrip
Event Description
ItemClicked, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

Properties of Menu Item


Property Description
Name, Text, TextAlign, BackColor, ForeColor, Font, Image, ImageAlign, Checked, Enabled, Visible,
ShortcutKeys, ShowShortcutKeys, Size, Location, BackgroundImage, BackgroundImageLayout,
ContextMenuStrip, BorderStyle
Events of Menu Item
Event Description
Click, DoubleClick, MouseMove, MouseEnter, MouseLeave

Application 92: Demo on MenuStrip

private void showToolStripMenuItem_Click(object sender, EventArgs e)


{
this.BackgroundImage = Image.FromFile("c:\\globe.jpg");
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = null;
}
private void normalToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}

private void minimizedToolStripMenuItem_Click(object sender, EventArgs e)


{
this.WindowState = FormWindowState.Minimized;
}

private void maximizedToolStripMenuItem_Click(object sender, EventArgs e)


{
this.WindowState = FormWindowState.Maximized;
}

private void form2ToolStripMenuItem_Click(object sender, EventArgs e)


{
Form2 f = new Form2();
f.Show();
}

private void form3Tool


StripMenuItem_Click(object sender, EventArgs e)
{
Form3 f = new Form3();
f.Show();
}

20) ContextMenuStrip
It is also a menu related control, similar to MenuStrip. But the context menu would be displayed,
when the user right clicks on a control or a form, at run time. This is also a collection of menu items.
The context
menu is also called as “Shortcut menu”. The “ContextMenuStrip” is an invisible control.

API: System.Windows.Forms.ContextMenuStrip
Naming Convension: conMnuxxxxxx

Properties of ContextMenuStrip
Property Description
Name, BackColor, Font, Enabled, Size, Location, BackgroundImage, BackgroundImageLayout,
ContextMenuStrip
Events of ContextMenuStrip
Event Description
ItemClicked, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

Application 93: Demo on ContextMenuStrip

Design

txtMyText

private void displayIconToolStripMenuItem_Click(object sender, EventArgs e)


{
if (displayIconToolStripMenuItem.Checked == true)
{
displayIconToolStripMenuItem.Checked = false;
this.ShowIcon = false;
}
else
{
displayIconToolStripMenuItem.Checked = true;
this.ShowIcon = true;
}
}

private void redToolStripMenuItem_Click(object sender, EventArgs e)


{
this.BackColor = Color.Red;
redToolStripMenuItem.Checked = true;
private void greenToolStripMenuItem_Click(object
greenToolStripMenuItem.Checked = false; sender, EventArgs e)
{ blueToolStripMenuItem.Checked = false;
} this.BackColor = Color.Green;
redToolStripMenuItem.Checked = false;
greenToolStripMenuItem.Checked = true;
blueToolStripMenuItem.Checked = false;
}

private void blueToolStripMenuItem_Click(object sender, EventArgs e)


{
this.BackColor = Color.Blue;

You might also like