Unit 3
Unit 3
We can add controls from the toolbox at design time as given below:
1. Dragging and Dropping
2. Double click on the control
We can add controls at run time as given steps below:
1. Create an object of control
2. Set the properties of it.
3. Add that controls to the form using Me.controls.Add(ctlobj).
Properties Description
Background Color It is used to change the back color of the control.
Fore Color It is used to change the fore color of the Control.
Visible It is used to enable or disable the visibility of the controls. It has
Boolean value either True or False. By default it is True.
Enabled This Property is similar to Visible property but the difference is that, In
this property the components are visible on both True and False
condition, but it not perform function on False condition.
Text Name (Text) of the control (i.e. Name which are display on the form)
we can change by using this property.
Dock By using this property we can arrange the controls (i.e. Top, Bottom,
Left, Right, and Fill) in a proper manner on the form.
Size It is used to set the size of the controls.
(Name) : This property is like a Text property. This property is used to set the
name of the control (i.e. the name which is used to writing a code on
control).
Font It is used to set the font style of the name of the control.
Image It is used to set the image of the controls.
Tab Stop It is declared in the form of Boolean i.e. in True or False value. This
allows the control focus on pressing Tab Key in Key Board.
Tab Index It is used to declare the index or the number on which control focus by
pressing Tab Key.
Faculty Name:Nayna N Mistry Page 1
Sutex Bank College of Computer Applications and Science
Unit 3: Introduction to Windows controls
3.1.1. Label
− It is used to display text on the form which user can not edit.
− However user can edit contents of the label control at run time using text property.
− The Prefix for the Label is “lbl”.
Properties:
Property Description
Method Description
Show It is used to show label control at run time.
Events:
Event Description
Click It is the default event of Label Control. It fires each time user clicks on Label
Control.
DoubleClick It fires each time user double clicks on Label Control.
Example: Program that accepts two numbers from user and performs addition, subtraction,
multiplication and division of two numbers using Label, Textbox and Button Control.
Source Code:
End Sub
3.1.2. Textbox
− It is used to accept textual input from the user. The user can add strings, numerical values
and a combination of those, but Images and other multimedia content are not supported.
− By default, it takes a single line of text, however, you can make it accept multiple texts and
even add scroll bars to it.The prefix of TextBox is “txt”
Properties:
Property Description
Name Indicates the name used in code to indentify the object.
Ex. txtname,txtaddress
Text Gets or sets the current text in the TextBox.
PasswordChar Indicates the character as a password.We can enter *? Etc character for
password
TextAlign It is used for setting text alignment such as Left,Right,Center
ScrollBars For adding scrollbars. ThisProperty has values
• None
• Horizontal
• Vertical
• Both
Multiline It is used to set the TextBox Control to allow multiple lines
MaxLength- It is used for specifying the maximum character number the TextBox
Control will accept.
Enabled It is used to specify whether textbox control is enabled or not at run
Faculty Name:Nayna N Mistry Page 4
Sutex Bank College of Computer Applications and Science
Unit 3: Introduction to Windows controls
Methods:
Method Description
Cut Moves the current selection in the text box to the Clipboard.
Copy Copies the current selection in the text box to the Clipboard.
Paste Replaces the current selection in the text box with the contents of the Clipboard.
Events:
Event Description
TextChanged It is the default event of TextBox Control. It fires each time a text in the
TextBox control is changed
GotFocus It fires each time a focus is set on TextBox
LostFocus It fires each time a focus is lost from TextBox
Example:
Source Code:
Public Class Frmtextbox
End Sub
End Sub
End Sub
End Sub
End Sub
End Sub
End Sub
End Class
3.1.3. Button
- Button is a widely used control in application. It is used to perform an action.
- Whenever user clicks on a button the click event associated with the button is fired and the
action associated with the event is executed.
- The prefix for button control is “btn”.
- We can set accelerator key for the button. It is also known as mnemonics. It is represented by
underlined characters.
- For example set text property of the btnans(button) as &Ans. It will display underline under
the character A now we call btnans button’s event by ALT +A.
Properties:
Property Description
Name Indicates the name used in code to indentify the object.
Ex. btnans,btnshow.
Text Gets or sets the current text in the Button.
TextAlign It is used for setting text alignment such as Left,Right,Center
Enabled It is used to specify whether textbox control is enabled or not at run
time. It has Boolean value. Default value is true.
TabIndex Gets or sets the tab order of the control within its container.
BackColor It is used to get or set background color.
Font It is used to set Font Face, Font Style, Font Size and Effects of the text.
Visible It is used to specify whether the control is visible or not at run time. It
has Boolean value. Default value is true.
FlatStyle It is used to get or set appearance of the Button Control when user
moves mouse on it or click on it. It has following 4 options:
System, Popup, Standard, Flat
Image It is used to specify an image that is displayed on it.
ImageAlign It is used to get or set alignment of the image that is displayed on it.
control.
TextImageRelation It is used to get or set position of text in relation with image. It has
following 5 options:
(1) Overlay
(2) ImageAboveText
Faculty Name:Nayna N Mistry Page 8
Sutex Bank College of Computer Applications and Science
Unit 3: Introduction to Windows controls
(3) TextAboveImage
(4) ImageBeforeText
(5) TextBeforeImage
It is used when user wants to display both text and image on Button
Control.
Methods:
Method Description
Show It is used to show Button control at run time.
Hide It is used to hide Button control at run time
Focus It is used to set input focus on Button Control at run time
Example: Refer the example of label or textbox
3.1.4. Checkbox
- It is used to set true/false or yes/no type options. The user can select or deselect it. When a
check box is selected it has the value True, and when it is deselected, it has the value False.
- The prefix of the checkbox is “chk”.
Properties:
Property Description
Text Gets or sets the current text of it.
Appearance It is used to determining the appearance of the check box.
Autocheck When check box is clicked it automatically change state (True/False). By default
it is true.
CheckAlign It Determine the location of the checkbox inside the control. By default it is
Middleleft.
Checked Indicating whether the check box is in checked state(True/False).
ThreeState Indicates whether check box will allow three check states rather than two.
CheckState Indicates the state of the checkbox.
There are 3 types of chechstate:
-checked
-unchecked
-indeterminate.
Events:
Event Descrption
CheckedChanged Raised when Checked property of the CheckBox control is changed
Example:
Source Code:
Public Class FrmCheckbox
Property Description
Text Gets or sets the current text of it.
TextAlign It is used for setting text alignment such as Left,Right,Center
Appearance It is used to determining the appearance of it (normal or button).
Enabled It is used to specify whether the control is enabled or not at run time. It has
Boolean value. Default value is true.
Visible It is used to specify whether the control is visible or not at run time. It has
Boolean value. Default value is true.
Autocheck When check box is clicked it automatically change state (True/False). By default
it is true.
CheckAlign It Determine the location of the checkbox inside the control. By default it is
Middleleft.
Checked Indicating whether the check box is in checked state (True/False).
3.1.6. Date Time Picker
It is used to select date and time.
Properties
Property Description
BackgroundImage It is used to set background Image for the DateTimePicker.
CalendarFont It is used to set the font style for the calendar in the DateTimePicker
control.
CustomFormat It is used to set the custom date and time format string in the
DateTimePicker control. dd/mm/yy
Checked It is used to check whether the value property is checked with a valid
date and time in the DateTimePicker control.
Format It is used to set the format for the Date and time displayed in the
Windows Form. dd/mm/yyyy
MaxDate It is used to set the maximum date value.
Name Indicates the name used in code to indentify the object.
MinDate It is used to set the minimum date value.
Value The current date and time value for this control.
Methods:
Method Description
Select() It is used to start or activate the DateTimePicker control.
Show() It is used to display the control.
ToString() It is used to return string that represent date and time.
Example:
ValueMember It is used to get or set the actual value for the items in the control.
Methods
Method Description
ClearSelected Unselects all the items in the ListBox.
FindString It returns the index of specified string. If it does not match then returns -1.
FindStringExact It returns the index of exactly matches of the specified string.
GetSelected It checks whether given index of item is selected or not. It returns Boolean
value. If item is selected then it returns true otherwise false.
SetSelected It selects or deselects the item of the given index
index.
Events
Event Description
Click It occurs when ListBox is selected
SelectedIndexChanged It occurs when SelectedInde
SelectedIndex property is changed
The Items in the list boxes are stored in the Item Collection. Properties and methods of Item
collection are given below.
Example:
Source Code
End Sub
lstcitynamedeg.SetSelected(4, True)
lstcitynamedeg.SetSelected(3, True)
End Sub
Properties
Property Description
DataBindings It gets the data binding for the control.
Name It represents unique name of control. It is used in the coding.
DataSource It is used to bind a collection of items to a ComboBox
DisplayMember It gets or sets the field to be displayed in the ComboBox.
ValueMember It is used to get or set the actual value for the items in the control.
Items It gets the items of the ComboBox.
SelectedIndex It returns the index of the selected item. If more than one item is
selected, it represents the lowest index. If no item is selected then it
returns -1.
SelectedItem It returns the text of selected item. If more than one item is selected,
then it returns the 1st Selected Item.
SelectedText Gets or sets the text that is selected in the editable portion of a
ComboBox
SelectedValue Gets or sets the value of the member property specified by the
ValueMember property.
Sorted Gets or sets a value indicating whether the items in the combo box
are sorted
IntegralHeight Indicates whether the combo box should resize to avoid showing
partial items.(true(Default)/false)
MaxDropDownItems The maximum number of entries to be display in the drop down list.
Methods
Method Description
SelectAll Selects all the text in the editable area of the combo box.
FindString It returns the index of specified string. If it does not match then returns -
1.
FindStringExact It returns the index of exactly matches of the specified string.
Events
Event Description
SelectedIndexChanged It occurs when SelectedIndex property is changed
Example:
cmbautofill.Items.Add("UK")
cmbautofill.Items.Add("Canada")
End Sub
End Class
3.1.9. Picture Box
− It is used for displaying images on the form. The Image property of the control allows you to
set an image both at design time and at run time.
Properties
Property Description
Name It represents unique name of control. It is used in the coding.
Image Gets or sets the image that is displayed in the control.
SizeMode
Determines the size of the image to be displayed in the control.
This property takes its value from the PictureBoxSizeMode
enumeration, which has values −
• Normal − the upper left corner of the image is placed at
upper left part of the picture box
• StrechImage − allows stretching of the image
• AutoSize − allows resizing the picture box to the size of
the image
• CenterImage − allows centering the image in the picture
box
• Zoom − allows increasing or decreasing the image size to
maintain the size ratio.
Events
Event Description
Click Raise when user clicks on it.
Example
Method Description
AppendText Appends text to the end of the control's text.
Cut Cuts the selected text to the Clipboard.
Copy Copies the selected text to the Clipboard.
Paste Replaces the current selection in the text box with the contents of the
Clipboard
Undo Undoes the last edit operation in the control.
Find Finds the specified text within the control.
LoadFile Loads the contents of a file into the control.
SaveFile Saves the contents of the control to a file.
Clear Clears all text from the control.
Example:
Source Code
Public Class FrmRichtextbox
End Sub
Private Sub btnselectall_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
richtext.SelectAll()
End Sub
Private Sub btnbaccolor_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnbaccolor.Click
richtext.SelectionBackColor = Color.Red
End Sub
Methods
Method Description
Add It adds nodes in the Tree view
Clear It clear all the nodes from the Tree view
Contains It checks whether the given node is a part of Tree view or not. It returns
Boolean value.
Insert We can insert node in the Tree View in any place by using this method. It
contains two arguments. Index and node
Remove It removes the node from the Tree View. The argument of Remove () method is
the node.
RemoveAt It removes the node from the Tree View by index.
GetNodeCount It is used to count the number of nodes that are available in the tree
view control.
ToString ToString method is used to return the name of the string that is in the
tree view control.
Example:
Source Code:
InitialDelay Gets or sets the time that passes before the ToolTip appears.
IsBaloon Gets or sets a value indicating whether the ToolTip should use a
balloon window.
ReshowDelay Gets or sets the length of time that must transpire before
subsequent ToolTip windows appear as the pointer moves from
one control to another.
ShowAlways Displays if tooltip is displayed even the parent control is not active.
ToolTipIcon Icon of tooltip window.
ToolTipTitle Title of tooltip window.
UseAnimation Represents weather an animation effect should be used when
displaying the tooltip.
UseFading Represents weather a fade effect should be used when displaying
the tooltip.
Example:
Properties
Property Description
Minimum It Get or Set Lower Bound of the range within which
ProgressBar Control works.
Maximum It Get or Set Upper Bound of the range within which
ProgressBar Control works.
Value It Get or Set current value of the ProgressBar within range
Example:
Timer1.Start()
End Sub
Timer1.Stop()
End Sub
Timer1.Stop()
ProgressBar1.Value = 0
End Sub
ProgressBar1.Step = 5
ProgressBar1.PerformStep()
'ProgressBar1.Increment(TextBox1.Text)
'Timer1.Interval = TextBox2.Text 'interval in minisecond .1000
milisecond =1 second'
End Sub
End Class
3.1.14. Timer Control
− It is used when user wants to perform some task or action continuously at regular interval
of time.
Properties
Property Name Description
Name It is used to specify name of the Timer Control.
Enabled It is used to determine whether Timer Control will be enabled or not.
It has Boolean value true or false. Default value is false.
Interval It is used to specify interval in millisecond. Tick event of Timer Control
generates after the time which is specified in Interval Property.
Method
Example
Properties:
Property Description
Mask It is used to get or set format string which determines whether
characters entered in MaskedTextBox are valid or not.
AllowPromptAsInput It is used to specify whether Prompt character can be entered as valid
input character in MaskedTextBox or not. It has Boolean value. Default
value is true.
AsciiOnly It is used to specify whether only ASCII characters can be entered as
valid input character in MaskedTextBox or not. It has Boolean value.
Default value is false.
BeepOnError It is used to specify whether control will generate system beep sound
on each invalid character input or not. It has Boolean value. Default
value is false.
PromptChar It is used to get or set Prompt character for MaskedTextBox Control.
This character is displayed in MaskedTextBox when user has not
entered any character
Text It is used to get or set text associated with it.
TextAlign It is used to get or alignment of the text associated with it.
TabIndex It is used to get or set Tab order of it.
TabStop It is used to specify whether user can use TAB key to set focus on it or
not. It has Boolean value. Default value is true.
Visible It is used to specify whether the Control is visible or not at run time. It
has Boolean value. Default value is true.
Methods:
Method Purpose
Append Text It is used to append text at the end of current text in it.
Clear It is used to clear all text from it.
Cut It is used to move current selection of it into clipboard.
Copy It is used to copies selected text of it into clipboard.
Paste It is used to replace current selection of MaskedTextBox by contents of
clipboard. It is also used to move contents of Clipboard to MaskedTextBox
control where cursor is currently located.
Select It is used to select specific text from it.
SelectAll It is used to select all text of it.
Event Description
MaskChanged It fires each time a mask property is changed.
TextChanged It fires each time a text in the MaskedTextBox control changed.
Example:
Mask
0 or 9 Number only
? Letter only
A Alphanumeric character
After that, click the ContextMenuStrip and add the following items into it.
After adding the items, double click the “Current Date and Time” to fire the click event handler
of it. Set the current date and time in the pop-up message.
After that, go back to the Form Design and double click the “Operating System (OS) Version” to
fire the click event handler of it. Set the current OS version in the pop-up message.
Go back to the Form Design again and double click the “exit” to fire the click event handler of it.
Put this code for closing the application.
Property Description
Icon Set the icon for the NotifyIcon.
ContentMenuStrip The short cut menu to show when the user right clicks the icon.
3.1.17.Link Label
− It is a label control that can display a hyperlink.
− Properties
Property Description
AutoSize Gets or sets a value indicating whether the control automatically adjusts its
size to fit its contents.
LinkArea Gets or sets the area of the control that is treated as a link.
LinkBehavior Gets or sets a value that specifies the behavior of the link.
Links Gets the collection of links in the control
LinkColor Gets or sets the color used for links in the control.
Text Gets or sets the text displayed by the control.
Methods:
Method Description
DoDragDrop Initiates a drag-and-drop operation.
Focus Sets input focus to the control.
OnLinkClicked Raises the LinkClicked event.
ToString Returns a string representation of the control.
Event:
Event Description
LinkClicked Triggered when the link is clicked.
Methods
Method Description
SetItemChecked(index, value) It sets the check state of the item at the specified index in the
CheckedListBox control to the specified value.
GetItemChecked(index) It gets the check state of the item at the specified index in the
CheckedListBox control.
GetItemCheckState(index) It gets the check state of the item at the specified index in the
CheckedListBox control and returns it as a CheckState
enumeration value.
SetItemCheckState(index, It sets the check state of the item at the specified index in the
state) CheckedListBox control to the specified CheckState
enumeration value.
GetItemText(index) It gets the text of the item at the specified index in the
CheckedListBox control.
FindStringExact(value) It searches for the item with the specified text and returns the
index of the first item found.
FindStringExact(value, It searches for the item with the specified text, starting from
startIndex) the specified index, and returns the index of the first item
Faculty Name:Nayna N Mistry Page 38
Sutex Bank College of Computer Applications and Science
Unit 3: Introduction to Windows controls
found.
GetSelected(index) It gets a value indicating whether the item at the specified
index is selected.
SetSelected(index, value) It sets a value indicating whether the item at the specified
index is selected.
ClearSelected() It clears the selection in the CheckedListBox control.
Event:
Event Description
SelectedIndexChanged It occurs when SelectedIndex property is changed
Example:
Source Code:
For i = 0 To clbcity.Items.Count - 1
clbcity.SetItemChecked(i, True)
Next
End Sub
− Group Box control: It is similar to the Panel control, but it includes a border and a caption,
which can be used to group related controls and to provide a visual separation between
different groups of controls.
Common Properties of Panel and Group Box
Property Description
BackColor Gets or sets the background color of the control.
BorderStyle Gets or sets the border style of the control.
Controls Gets the collection of controls contained within the control.
Padding: Gets or sets the padding within the control.
AutoScroll: Gets or sets a value indicating whether the control enables automatic
scrolling when the control is resized.
AutoSize Gets or sets a value indicating whether the control is automatically
resized to fit its contents.
Enabled Gets or sets a value indicating whether the control is enabled.
Visible Gets or sets a value indicating whether the control is visible.
− Both the Panel and GroupBox controls have similar properties, such as BackColor, Controls,
Padding, AutoSize, Enabled, and Visible. However, the GroupBox control also has a Text
property that allows you to set the caption or title of the group box.
Property Description
Images Gets the collection of images in the image list.
ColorDepth Gets or sets the color depth of the images in the image list.
TransparentColor Gets or sets the color that is treated as transparent.
ImageSize Gets or sets the size of the images in the image list.
Tag Gets or sets an object that contains data about the control.
Methods:
Method Description
Clear() Removes all the images from the image list.
Add(key As String, image As Image) Adds a new image to the image list, using the specified
key to identify it.
IndexOfKey(key As String) Returns the index of the image with the specified key.
Faculty Name:Nayna N Mistry Page 41
Sutex Bank College of Computer Applications and Science
Unit 3: Introduction to Windows controls
RemoveByKey(key As String) Removes the image with the specified key from the
image list.
Method Description
Disposed Occurs when the control is disposed of by a call to the Dispose
method.
ItemAdded Occurs when an item is added to the image list.
ItemRemoved: Occurs when an item is removed from the image list.
− you can create an image list by using the ImageList control. Here are the basic steps:
− Add an ImageList control to your form.
− Add images to the ImageList control by either:
− a. Setting the Images property in the Properties window and manually adding
images.
− b. Adding images programmatically using the ImageList.Images.Add method.
− Use the ImageList control in other controls that support images, such as PictureBox and
ListView.
− Example
Source Code:
Method Description
SetError It is used to set error message for particular control.
Ex:ErrorProvider1.SetError(ControlName,"ErrorMessage")
GetError It is used to retrieve current error message of particular control.
Ex: ErrorProvider1.GetError (ControlName).
Clear It is used to clear all the settings of ErrorProvider Control.
Ex:ErrorProvider1.Clear()
Example:
Source Code:
Now Double click on Submit Button and write following code in Click event.
ErrorProvider1.Clear()
If txtName.Text = "" Then
ErrorProvider1.SetError(txtName, "Please Enter Name")
txtName.Focus()
lblname.Text = ErrorProvider1.GetError(txtname)
Else
lblName.Text = txtName.Text
End If
3.1.4.3 Help Provider
− It provides popup or online help for a control.
Properties and Methods:
Properties and Description
Methods:
SetShowHelp It specifies whether Help is displayed for the specified control
SetHelpString It specifies the Help string associated with the specified control
SetHelpNavigator It specifies the Help command to use when retrieving Help from the
Help file for the specified control
SetHelpKeyword It specifies the keyword used to retrieve Help when the user invokes
Help for the specified control
HelpNamspace gets or sets a value specifying the name of the Help file associated
with this HelpProvider
Example:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Help.ShowHelp(Label1, "f:/help.html")
End Sub
Context Menu
− Menu items which are available when you right click are called Context Menus.
− Steps of Context Menu
− Drag and drop context menu strip control to the textbox control.
− When you do, you will notice two things. At the top of your form, you will see this. And
type cut copy and paste options. Then set the ContextMenuStrip Property of the object.
Common Dialogue Boxes (Refer From book (Dr. Shyam N.Chawda) Page No 230)
m = TextBox2.Text
ans = n / m
MsgBox(ans)
Catch ex As DivideByZeroException
MsgBox(ex.Message)
Catch ex As InvalidCastException
MsgBox(ex.Message)
Catch ex As Exception
MsgBox(ex.Message)
Finally
MsgBox("Hi in finally")
End Try
End Sub
OR
GoTo line | Label—Calls the error-handling code that starts at the line specified at line. Here,
line is a line label or a line number. If a runtime error occurs, program execution goes to the
given location. GoTo 0—Disables the enabled error handler in the current procedure and reset
it to nothing. It clears the error object.
GoTo -1—Same as GoTo 0.
Resume Next—specifies that when an exception occurs, execution skips over the statement
that caused the problem and goes to the statement immediately following and Execution
continues from that point.
Err Object
When an error occurs, the Err object contains information about the error. This helps you to
determine whether you can attempt to fix the error or ignore the error.
Properties:
Property Description
Number The error number. If this is zero then no error has occurred..
Description A short description of the error.
HelpFile A folder location and filename of the help file.
Source The project name from the Properties dialog box.
HelpContext The context ID for a particular error in a help file.
Methods:
Property Description
Clear It clears the Err Object
Raise It raises an error.
Example:
Note: For more Details regarding all the topics in vb.net, you might have to refer the books.