Unit II
Unit II
Syntax
Format I :
The following syntax shows the use of a message box that is intended to return a value:
DialogResult = MessageBox.Show (Text, Caption, Buttons, Icon, DefaultButton, Options)
• Text contains the text that will be displayed in the message box. It is a string, so enclose an
alphanumeric sequence in quotation marks, as in, "I am a string!"
• Caption, which is optional, contains the text that appears in the caption ba.
• Buttons, which is optional, tells VB .NET which buttons will be displayed.
• Icon, which is optional, tells VB .NET which icon will be displayed.
• DefaultButton, which is optional, tells VB .NET which button is activated by default (when
the user presses the Enter key).
• Options, which is optional, allows you to select some special options for the message box.
These include things like making the text right-aligned, specifying a right-to-left reading
order, or displaying a message box from a Windows service application.
Example
Dim answer As DialogResult
answer = MessageBox.Show("I return a value")
Format II
The following syntax shows the use of a message box without returning value:
MessageBox.Show (Text, Caption, Buttons, Icon)
Example
We also need to know what the possible return values are for MessageBox.Show
The Icon argument, which sets the icon that is displayed, is specified using one of the
members that are part of the MessageBoxIcon set of values.
Displaying OK and Cancel Buttons
To add an icon to the display as well as a button, simply add an argument in the Icon position:
MessageBox.Show("Press OK to continue, Cancel to escape!", "Are you tired of this application
yet?", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
The result is a message box with an information icon, along with OK and Cancel buttons.
We can easily display Yes, No, and Cancel buttons. Here's the code that creates the box, and returns
a value:
Dim answer As DialogResult
answer = MessageBox.Show("Press Yes for lunch, No for dinner and Cancel if you are not
hungry!", "What meal is it?", MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question)
The resulting message box includes a question icon and Yes, No, and Cancel buttons.
Methods
A method is a procedure created as a member of a class and they cause an object to do something.
Methods are used to access or manipulate the characteristics of an object or a variable. There are
mainly two categories of methods you will use in your classes −
• If you are using a control such as one of those provided by the Toolbox, you can call any of
its public methods. The requirements of such a method depend on the class being used.
• If none of the existing methods can perform your desired task, you can add a method to a class.
For example, the MessageBox control has a method named Show, which is called in the code snippet
below −
Public Class Form1
Private Sub Button1_Click()
MessageBox.Show("Hello, World")
End Sub
End Class
Control Events
An event is a signal that informs an application that something important has occurred. For
example, when a user clicks a control on a form, the form can raise a Click event and call a procedure
that handles the event. There are various types of events associated with a Form like click, double
click, close, load, resize, etc.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'event handler code goes here
End Sub
• BackColor and ForeColor properties are used to set background and foreground color of a
GroupBox respectively. If you click on these properties in Properties window, the Color
Dialog pops up.
• Alternatively, you can set background and foreground colors at run-time. The following code
snippet sets BackColor and ForeColor properties.
Groupbox1.BackColor = Color.LightBlue
Groupbox1.ForeColor = Color.Maroon
Text
• Text property of a GroupBox represents the header text of a GroupBox control. The following
code snippet sets the header of a GroupBox control.
• Font property represents the font of header text of a GroupBox control. If you click on the
Font property in Properties window, you will see Font name, size and other font options. The
following code snippet sets Font property at run-time.
• Name property represents a unique name of a GroupBox control. It is used to access the
control in the code. The following code snippet sets and gets the name and text of a GroupBox
control.
Groupbox1.Name = "GroupBox1"
• The Location property takes a Point that specifies the starting position of the GroupBox on a
Form. The Size property specifies the size of the control. We can also use Width and Height
property instead of Size property.
Groupbox1.Location = New Point(10, 10)
Groupbox1.Width = 250
Groupbox1.Height = 200
Label Controls
• It is generally used to display some informative text on the GUI which is not changed during
runtime.
• Let's create a label by dragging a Label control from the Toolbox and dropping it on the form.
VB.NET Label Properties
Properties Description
AutoSize As the name defines, an AutoSize property of label control is used to set or get
a value if it is automatically resized to display all its contents.
Border Style It is used to set the style of the border in the Windows form.
PreferredWidth It is used to set or get the preferred width for the Label control.
Font It is used to get or set the font of the text displayed on a Windows form.
TextAlign It is used to set the alignment of text such as centre, bottom, top, left, or right.
ContextMenu It is used to get or sets the shortcut menu associated with the Label control.
ImageIndex It is used to set the index value to a label control displayed on the Windows
form.
Events Description
AutoSizeChanged An AutoSizeChanged event occurs in the Label control when the value of AutoSize
property is changed.
DoubleClick When a user performs a double-clicked in the Label control, the DoubleClick event
occurs.
GotFocus It occurs when the Label Control receives focus on the Window Form.
Leave The Leave event is found when the input focus leaves the Label Control.
TabIndexChanged It occurs when the value of Tabindex property is changed in the Label control.
TabStopChanged It occurs when the property of TabStop is changed in the Label Control.
BackColorChanged A BackColorChanged event occurs in the Label control when the value of the
BackColor property is changed.
DragDrop A DragDrop event occurs in the Label control when a drag and drop operation is
completed.
1 GetPreferredSize
Retrieves the size of a rectangular area into which a control can be fitted.
2 Refresh
Forces the control to invalidate its client area and immediately redraw itself and any child
controls.
3 Select
Activates the control.
4
Show
Displays the control to the user.
5 ToString
Returns a String that contains the name of the control.
• A TextBox control is used to display, accept the text from the user as an input, or a single line
of text on a VB.NET Windows form at runtime. Furthermore, we can add multiple text and
scroll bars in textbox control.
• We have to drag the TextBox control from the Toolbox and drop it on the Windows form, as
shown below.
TextBox Properties
Once the TextBox is added to the form, we can set various properties of the TextBox by clicking on
the TextBox control.
Properties Description
Font It is used to set the font style of the text displayed on a Windows form.
Multiline It is used to enter more than one line in a TextBox control, by changing
the Multiline property value from False to True.
PasswordChar It is used to set the password character that can be a mask in a single line
of a TextBox control.
PreferredHeight It is used to set the preferred height of the textbox control in the window
form.
Text It is used to get or set the text associated with the textbox control.
Visible The Visible property sets a value that indicates whether the textbox
should be displayed on a Windows Form.
TextBox Methods
1 AppendText
Appends text to the current text of a text box.
2 Clear
Clears all text from the text box control.
3 Copy
Copies the current selection in the text box to the Clipboard.
4 Cut
Moves the current selection in the text box to the Clipboard.
5 Paste
Replaces the current selection in the text box with the contents of the Clipboard.
6 Paste(String)
Sets the selected text to the specified text without clearing the undo buffer.
7
ResetText
Resets the Text property to its default value.
8 ToString
Returns a string that represents the TextBoxBase control.
9
Undo
Undoes the last edit operation in the text box.
TextBox Events
Events Description
BackColorChanged It is found in the TextBox Control when the property value of the
BackColor is changed.
MouseClick A MouseClick event occurs when the mouse clicks the control.
• The RadioButton is used to select one option from the number of choices. If we want to select
only one item from a related or group of items in the windows forms, we can use the radio
button.
• The RadioButton is mutually exclusive that represents only one item is active and the remains
unchecked in the form.
• If we need to place more than one group of radio buttons in the same form, we should place
them in different container controls like a GroupBox control.
• The Checked property of the radio button is used to set the state of a radio button.
• Let's create three radio buttons by dragging RadioButton controls from the Toolbox and
dropping on the form.
Properties of the RadioButton Control
Property Description
Appearance It is used to get or set a value that represents the appearance of the
RadioButton.
AutoCheck The AutoCheck property is used to check whether the checked value or
appearance of control can be automatically changed when the user clicked
on the RadioButton control.
AutoSize The AutoSize property is used to check whether the radio control can be
automatically resized by setting a value in the RadioButton control.
Events Description
Let us create two groups of radio buttons and use their CheckedChanged events for changing the
BackColor and ForeColor property of the form.
Using IF Statements
• In VB.NET, the control statements are the statements that controls the execution of the
program on the basis of the specified condition.
• It is useful for determining whether a condition is true or not.
• If the condition is true, a single or block of statement is executed.
• VB.NET provides the following conditional or decision-making statements.
✓ If-Then Statement
✓ If-Then Else Statement
✓ If-Then ElseIf Statement
If-Then Statement
• The If-Then Statement is a control statement that defines one or more conditions, and if the
particular condition is satisfied, it executes a piece of information or statements.
• The If-Then Statement can execute single or multiple statements when the condition is true,
but when the expression evaluates to false, it does nothing
Syntax:
If condition Then
[Statement or block of Statement]
End If
If-Then-Else Statement
If the condition is true, the if code statement will execute, and if the condition is false, Else code will
be executed. After that, the control transfer to the next statement, which is immediately after the If-
Then-Else control statement.
Syntax:
If condition Then
'This statement will execute if the Boolean condition is true
Else
'Optional statement will execute if the Boolean condition is false
End If
If-Then-ElseIf statement
• The If-Else-If statement or condition is useful for defining the multiple conditions and
executing only the matched condition based on our requirements.
• Execution starts from the top to bottom, and it checked for each If condition. And if the
condition is met, the block of If the statement is executed. And if none of the conditions are
true, the last block is executed.
Syntax
If (condition 1) Then
' Executes when condition 1 is true
ElseIf ( condition 2) Then
' Executes when condition 2 is true
ElseIf ( condition 3) Then
' Executes when the condition 3 is true
Else
' executes the default statement when none of the above conditions is true.
End If
Example
Finding Biggest Number
Private Sub Button1_Click()
If val(Textbox1.Text) > val(Textbox2.Text) then
Textbox3.text=val(Textbox1.Text)
Else
Textbox3.Text=val(Textbox1.Text)
End if
End Sub