[go: up one dir, main page]

0% found this document useful (0 votes)
30 views17 pages

Unit II

Uploaded by

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

Unit II

Uploaded by

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

UNIT II

Displaying Message Box


• It is a dialog box which displays a pop-up style message box and waits for the user to click a
button, and then an action is performed based on the clicked button by the user.
• Displays a message in a dialog box, waits for the user to click a button, and returns an Integer
indicating which button the user clicked.

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

MessageBox.Show("This displays a message")


The MsgBox Function
MsgBox works in much the same way as MessageBox.Show. Here is an example of a MsgBox
statement:
MsgBox ("Hello, World!")
This code displays a message box containing the text "Hello, World!".

Displaying Text and a Caption


• To display text and a caption only, use code like this:
MessageBox.Show("This is the text!", "This is the caption!")
The message box displays text, a caption, and an OK button, as shown here.

Adding Buttons and Icons to Message Boxes


The Buttons argument, which controls the buttons that are displayed, is set using one of the
enumeration constants, or members. When you start typing MessageBox.Show in the Code Editor,
we can choose a MessageBoxButtons enumeration value from the drop-down list.

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.

Displaying Yes, No, 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.

Setting the Initially Selected Button


The fifth MessageBox.Show argument lets you specify the default button, which is the button that is
initially selected in a message box. We have three choices, working from left to right:
MessageBoxDefaultButton.Button1 selects the first button,
MessageBoxDefaultButton.Button2 selects the second button, and, as you would suppose,
MessageBoxDefaultButton.Button3 selects the third button.
If you don't use the argument, the first button is selected by default.
To move the initial selection to the second button, you could use the following code:
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, MessageBoxDefaultButton.Button2)
The resulting message box will include Yes, No, and Cancel buttons, with No (the second button)
selected by default.

Sixth argument – Options


We can add the constant MessageBoxOptions.RightAlign to the sixth MessageBox.Show argument,
as in this example:
Dim answer As DialogResult
answer = MessageBox.Show(“I am on the right”, MessageBoxButtons.OK,
MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
This aligns the text in the message box on the right.

Adding Form Controls


The controls are an object in a form such as buttons, Textboxes, Textarea, labels, etc. to perform some
action and to create user interface.
Visual Basic control consists of three important elements −
• Properties which describe the object,
• Methods cause an object to do something and
• Events are what happens when an object does something.
Properties
All the Visual Basic Objects can be moved, resized or customized by setting their properties. A
property is a value or characteristic held by a Visual Basic object, such as Caption or Fore Color.
Properties can be set at design time by using the Properties window or at run time by using statements
in the program code.
Object. Property = Value
Where
• Object is the name of the object you're customizing.
• Property is the characteristic you want to change.
• Value is the new property setting.
For example,
Form1.Caption = "Hello"

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

Adding Group Boxes


• GroupBoxes and Panels are used to hold, or contain, groups of other controls
• A GroupBox control is a container control that is used to place Windows Forms child controls
in a group. The purpose of a GroupBox is to define user interfaces where we can categories
related controls in a group.
Creating a GroupBox
• To create a GroupBox control at design-time, we simply drag and drop a GroupBox control
from Toolbox to a Form in Visual Studio. After you drag and drop a GroupBox on a Form,
the GroupBox looks like given Figure.
• Once a GroupBox is on the Form, you can move it around and resize it using mouse and set
its properties and events.
Setting GroupBox Properties
• The easiest way to set properties is from the Properties Window.

Background and Foreground

• 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.

Groupbox1.Text = "Author Details"


Font

• 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.

Groupbox1.Font = New Font("Georgia", 12)


Name

• 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"

Location, Height, Width, and Size

• 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

Adding Labels and Textboxes

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.

PreferredHeight It is used to set the height for the Label Control.

TextAlign It is used to set the alignment of text such as centre, bottom, top, left, or right.

ForeColor It is used to set the color of the text.

Text It is used to set the name of a label in the Windows Form.

ContextMenu It is used to get or sets the shortcut menu associated with the Label control.

DefaultSize It is used to get the default size of the Label control.

Image It is used to set the image to a label in Windows Form.

ImageIndex It is used to set the index value to a label control displayed on the Windows
form.

VB.NET Label Events

Events Description

AutoSizeChanged An AutoSizeChanged event occurs in the Label control when the value of AutoSize
property is changed.

Click Click event is occurring in the Label Control to perform a click.

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.

ControlRemoved When the control is removed from the Control.ControlCollection, a


ControlRemoved event, occurs.

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.

ControlAdded When a new control is added to the Control.ControlCollection, a ControlAdded


event occurs.

DragDrop A DragDrop event occurs in the Label control when a drag and drop operation is
completed.

Methods of the Label Control

Sr.No. Method Name & Description

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.

Adding Text Boxes

• 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.

Lines It is used to set the number of lines in a TextBox control.

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.

ScrollBars It is used to display a scrollbar on a multiline textbox by setting a value


for a Textbox control.
• None
• Horizontal
• Vertical
• Both

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.

WordWrap The WordWrap properties validate whether the multiline Textbox


control automatically wraps words to the beginning of the next line when
necessary.
TextAlign Gets or sets how text is aligned in a TextBox control. This property has
values – Left, Right and Center

ForeColor Gets or sets the foreground color of the control.

TextLength Gets the length of text in the control.

TextBox Methods

Sr.No. Method Name & Description

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

Click When a textbox is clicked, a click event is called in the textbox


control.

BackColorChanged It is found in the TextBox Control when the property value of the
BackColor is changed.

BorderStyleChanged It is found in the TextBox Control when the value of the


BorderStyle is changed.

FontChanged It occurs when the property of the Font is changed.

GetFocus It is found in TextBox control to get the focus.

MouseClick A MouseClick event occurs when the mouse clicks the control.

MultilineChanged It is found in a textbox control when the value of multiline


changes.

Example login Form

Adding Radio Buttons

• 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.

Text Gets or sets the caption for a radio button..

Gets or sets a value indicating whether the control is checked.


Checked

Events of the RadioButton Control

Events Description

Occurs when the value of the Appearance property of the


AppearanceChanged
RadioButton control is changed.

Occurs when the value of the Checked property of the


CheckedChanged
RadioButton control is changed.
Example

Let us create two groups of radio buttons and use their CheckedChanged events for changing the
BackColor and ForeColor property of the form.

Private Sub Button1_Click()


If RadioButton1.Checked = True Then
Me.BackColor=Color.Red
ElseIf RadioButton2.Checked = True Then
Me.BackColor=Color.Green
Else
Me.BackColor=Color.Blue
End If
End Sub

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

You might also like