More Related Content
Similar to Vb.net session 02
Similar to Vb.net session 02 (20)
Vb.net session 02
- 1. Working With Windows Forms
Pre-Assessment Questions
1. Which of the following is not included in the user interfaces provided by .NET
framework?
a. Web forms
b. Windows forms
c. Console applications
d. Web services
1. Which of the following information is not stored in Assemblies?
a. Name of the assembly
b. Version number of the assembly
c. Security information
d. Type-safety checks
©NIIT Introduction to VB .NET Lesson 1B / Slide 1 of 43
- 2. Working With Windows Forms
Pre-Assessment Questions (Contd.)
1. The Visual Studio .NET IDE provides three navigational features. Which of
the following is not the navigational feature provided by .NET?
a. Docking
b. Tabbed Navigation
c. Auto Hide
d. Auto Visible
1. CLR enforces restrictions on managed code to provide Code Access Security.
This is done through the use of objects. These objects are called
a. GAC
b. CTS
c. CLS
d. Permissions
©NIIT Introduction to VB .NET Lesson 1B / Slide 2 of 43
- 3. Working With Windows Forms
Pre-Assessment Questions (Contd.)
1. The capability of a derived class to hide the characteristics of the parent class
is called
a. Overriding
b. Overloading
c. Structured programming
d. Inheritance
©NIIT Introduction to VB .NET Lesson 1B / Slide 3 of 43
- 4. Working With Windows Forms
Solutions to Pre-Assessment
Questions
1. d.
2. d.
3. d
4. d.
5. a.
©NIIT Introduction to VB .NET Lesson 1B / Slide 4 of 43
- 5. Working With Windows Forms
Objectives
In this lesson, you will learn to:
• List the features of Windows Forms
• Identify different form controls
• List the different language features required to work with Windows Forms
• Create Windows Forms
• Validate data in Windows Forms
©NIIT Introduction to VB .NET Lesson 1B / Slide 5 of 43
- 6. Working With Windows Forms
Getting Started With Windows Forms
• A user interface is the means by which a user interacts with an application.
• Two types of user interfaces are:
• Character user interface (CUI):
• Graphical user interface (GUI):
• User interaction in a Windows-based application is accomplished through
Windows Forms and controls.
©NIIT Introduction to VB .NET Lesson 1B / Slide 6 of 43
- 7. Working With Windows Forms
Introducing Windows Forms
• A Windows Form
• is a representation of any window displayed in an application.
• is used to accept input from a user and display information.
• is a class derived from Form class in System.Windows.Forms namespace.
• can also be inherited from the existing forms in the project to change the
existing behavior.
• inherits many predefined properties, methods, and events associated with
its base classes.
©NIIT Introduction to VB .NET Lesson 1B / Slide 7 of 43
- 8. Working With Windows Forms
Introducing Windows Forms (Contd.)
The following figure shows a sample Windows Form for accepting login details from a
user:
When you create a new project for a Windows application, a form is automatically
added to the project. This form has the default name Form1.vb.
©NIIT Introduction to VB .NET Lesson 1B / Slide 8 of 43
- 9. Working With Windows Forms
Windows Forms Properties
• Windows Form properties are used to determine its appearance at run time.
• The commonly used properties of a Windows Form are:
• Name
• BackColor
• BackgroundImage
• Font
• Size
• StartPosition
• Text
• WindowState
©NIIT Introduction to VB .NET Lesson 1B / Slide 9 of 43
- 10. Working With Windows Forms
Windows Forms Events
• An event is generated when a user performs an action, such as clicking the
mouse or pressing a key.
• Each form and control has a predefined set of events associated with it.
• The common events used in a Windows Form are:
• Click
• Closed
• Deactivate
• Load
• MouseMove
• MouseDown
• MouseUp
©NIIT Introduction to VB .NET Lesson 1B / Slide 10 of 43
- 11. Working With Windows Forms
Windows Forms Events (Contd.)
• Event handler is a special method that specifies the action to be performed on
the occurrence of an event.
• You can add the code for an event handler by using the Code Editor window
as shown in the following figure:
©NIIT Introduction to VB .NET Lesson 1B / Slide 11 of 43
- 12. Working With Windows Forms
Windows Forms Events (Contd.)
• The following example shows an event handler for the Load event of Form1:
Private Sub Form1_Load(ByVal sender
As System.Object, ByVal e As
System.EventArgs) Handles
MyBase.Load
’Code for initialization
of form variables
End Sub
• In the given sample code:
• sender provides a reference to the object that raised the event
• e passes an object-specific input, such as the location of the mouse and
the type of mouse event for the event being handled.
©NIIT Introduction to VB .NET Lesson 1B / Slide 12 of 43
- 13. Working With Windows Forms
Windows Forms Methods
• Windows Form methods allow you to perform various tasks, such as opening,
activating, and closing the form.
• The commonly used methods are:
• Show()
• Activate()
• Close()
• SetDesktopLocation()
©NIIT Introduction to VB .NET Lesson 1B / Slide 13 of 43
- 14. Working With Windows Forms
Form Controls
• A Form Control
• is a component used to accept input from the user or display some
information on the form.
• has its own set of properties, methods, and events that makes it suitable
for a particular task.
• can be added to a Windows Form by dragging the control from the toolbox
and placing it on the form at the required location.
• can also be loaded on the Windows Form by double-clicking the control on
the toolbox.
©NIIT Introduction to VB .NET Lesson 1B / Slide 14 of 43
- 15. Working With Windows Forms
Form Controls (Contd.)
The following figure shows some controls:
TextBox Control- Multiline
property set
Label
Control TextBox
Control
ListBox
Control
GroupBox
Control
CheckBox
Control
Button
Control
StatusBar
Control
©NIIT Introduction to VB .NET Lesson 1B / Slide 15 of 43
- 16. Working With Windows Forms
The TextBox Control
• The TextBox control is used to display text to a user or accept input from a
user.
• By default, you can enter a maximum of 2048 characters in a TextBox
control.
• The commonly used properties of the TextBox control are:
• Text
• MultiLine
• PasswordChar
©NIIT Introduction to VB .NET Lesson 1B / Slide 16 of 43
- 17. Working With Windows Forms
The Label Control
• The Label control
• is used to display the text that cannot be edited by the user directly.
• is used for providing information or description of another control on the
Windows Form.
• The commonly used properties of the Label control are:
• Text
• AutoSize
©NIIT Introduction to VB .NET Lesson 1B / Slide 17 of 43
- 18. Working With Windows Forms
The LinkLabel Control
• The LinkLabel control is used to display the text as a link to another form or
Web site.
• The LinkLabel control has all properties of a Label control.
• The commonly used additional properties are:
• LinkColor
• ActiveLinkColor
• DisabledLinkColor
• LinkVisited
©NIIT Introduction to VB .NET Lesson 1B / Slide 18 of 43
- 19. Working With Windows Forms
The ListBox Control
• The ListBox control is used to display a list of items to a user.
• A user can select an item from the list.
• You can add items to a ListBox control by using the properties window.
• The commonly used properties of the ListBox control are:
• SelectionMode
• Sorted
• SelectedIndex
• SelectedItem
©NIIT Introduction to VB .NET Lesson 1B / Slide 19 of 43
- 20. Working With Windows Forms
The ComboBox Control
• The ComboBox control
• is used to display a drop-down list of items.
• is a combination of a text box in which a user enters an item and a drop-
down list from which a user selects an item.
• can be populated using the Properties window.
• The commonly used properties of the ComboBox control are:
• Text
• Sorted
• SelectedIndex
• SelectedItem
©NIIT Introduction to VB .NET Lesson 1B / Slide 20 of 43
- 21. Working With Windows Forms
The CheckBox Control
• The CheckBox control is used to set Yes/No or True/false options.
• The commonly used properties of the CheckBox control are:
• Text
• Checked
©NIIT Introduction to VB .NET Lesson 1B / Slide 21 of 43
- 22. Working With Windows Forms
The RadioButton Control
• The RadioButton control is used to provide a set of mutually exclusive options
to the user.
• Only one radio button can be selected in a group.
• The commonly used properties of the RadioButton control are:
• Text
• Checked
©NIIT Introduction to VB .NET Lesson 1B / Slide 22 of 43
- 23. Working With Windows Forms
The GroupBox Control
• The GroupBox control
• is used to group related controls together.
• facilitates the group of controls to be easily moved together at design
time.
• You can specify the caption to be displayed for a GroupBox control by setting
the Text property at runtime as follows:
GroupBox1.Text = “Select Gender”
©NIIT Introduction to VB .NET Lesson 1B / Slide 23 of 43
- 24. Working With Windows Forms
The Button Control
• The Button control is used to perform an action when a user clicks it.
• The Button control has the Text property, which is used to display text on a
button.
• You can also set the Text property at run time, as shown below:
GroupBox1.Text = “Select Gender”
©NIIT Introduction to VB .NET Lesson 1B / Slide 24 of 43
- 25. Working With Windows Forms
The StatusBar Control
• The StatusBar control is used to display the status information at the bottom
of the form.
• The commonly used properties of the StatusBar control are:
• ShowPanels
• Text
• You can display different text in different panels of a StatusBar control by
following steps:
• set the ShowPanels property to True.
• add the text for each panel of the StatusBar control by using the Panels
collection.
©NIIT Introduction to VB .NET Lesson 1B / Slide 25 of 43
- 26. Working With Windows Forms
Form Controls (Contd.)
• The events associated with various controls can be categorized as follows:
• Keyboard events
• KeyDown
• KeyUp
• KeyPress
• Mouse events
• MouseDown
• MouseUp
• MouseMove
• Control-specific events
• Resize
• VisibleChanged
©NIIT Introduction to VB .NET Lesson 1B / Slide 26 of 43
- 27. Working With Windows Forms
Dynamically Loading Controls in
Windows Forms
• The loading of controls at runtime is called as dynamic loading of controls.
• Every control is a class derived from the System.Windows.Forms.Control
class.
• Whenever you load a control from the toolbox in the form, an object of the
class is created.
• Example: To dynamically load a text box, you need to specify the following
code in the Form1_Load event of the Windows Form:
Dim Txt1 As TextBox = New TextBox()
Form1.Controls.Add(Txt1)
The text box, Txt1 will be shown in the upper-left corner of the Form1.
©NIIT Introduction to VB .NET Lesson 1B / Slide 27 of 43
- 28. Working With Windows Forms
Introducing VB .NET Language
Fundamentals
• VB .NET provides a number of language features. The fundamental
features are:
• Data types
• Variables
• Arrays
• Operators
• Control Flow Constructs
©NIIT Introduction to VB .NET Lesson 1B / Slide 28 of 43
- 29. Working With Windows Forms
Data Types
• The data used by an application may be of different types.
• The important data types provided by VB .NET to store different types
of data are:
• Byte
• Short
• Integer
• Long (long integer)
• Single
• Double
• Decimal
• Boolean
• Char
• String
• Date
• Object
©NIIT Introduction to VB .NET Lesson 1B / Slide 29 of 43
- 30. Working With Windows Forms
Variables
• VB .NET allows you to use variables for temporarily storing data at run time.
• A variable has a
• Name: used to access and manipulate the value of the variable.
• Data type: determines the type of data that the variable can store.
• The Dim statement is used to declare variables.
• You can specify the name of a variable and the type of storage in the Dim
statement.
• The data type for the variable is defined by using the As clause of the Dim
statement.
A variable can be defined using:
• A system data type, such as Boolean, String, or Integer
• A composite data type, such as an array or a structure
• An object type, such as Label or TextBox
©NIIT Introduction to VB .NET Lesson 1B / Slide 30 of 43
- 31. Working With Windows Forms
Arrays
• An array is a collection of values of the same data type.
• The variables in an array are called array elements.
• Array elements are accessed using a single name and an index number
representing the position of the element within the array.
• In VB .NET, the array subscript always starts with zero.
©NIIT Introduction to VB .NET Lesson 1B/ Slide 31 of 43
- 32. Working With Windows Forms
Arrays (Contd.)
• Declaring an Array: You can declare an array by using the following syntax:
Dim arrayname(Number-of-elements) as datatype
You can also create multidimensional arrays.
• Assigning Values to Array Elements: You can assign values to each element
of an array by using the index number (also called the array subscript) of the
element. Example: Score(0) = 5
This statement assigns the value 5 to the first element of the array, Score.
• Resizing an Array: You can resize an array by using the ReDim statement.
However, when you use the ReDim statement, the existing contents are
erased. You can use the Preserve keyword to ensure that the existing
contents of an array are not lost when you use the ReDim statement.
Example: ReDim Score(20)
This statement resizes the Score array to hold 20 elements.
©NIIT Introduction to VB .NET Lesson 1B/ Slide 32 of 43
- 33. Working With Windows Forms
Operators
• Applications use operators to process the data entered by a user.
• Arithmetic Operators: Arithmetic operators are used to perform arithmetic
operations on variables.
• +
• -
• *
• ^
• /
•
• Mod
©NIIT Introduction to VB .NET Lesson 1B / Slide 33 of 43
- 34. Working With Windows Forms
Operators (Contd.)
• Comparison Operators: Comparison operators are used to compare two
values and perform an action based on the result of the comparison.
• <
• >
• <=
• >=
• =
• <>
©NIIT Introduction to VB .NET Lesson 1B / Slide 34 of 43
- 35. Working With Windows Forms
Operators (Contd.)
• Logical operators: Logical operators are used to evaluate expressions and
return a Boolean value.
• And
• Not
• Or
• Xor
• OrElse
• AndAlso
©NIIT Introduction to VB .NET Lesson 1B / Slide 35 of 43
- 36. Working With Windows Forms
Operators (Contd.)
• Concatenation Operators: Concatenation Operators are used to join two
expressions.
• &
• +
©NIIT Introduction to VB .NET Lesson 1B / Slide 36 of 43
- 37. Working With Windows Forms
Control Flow Constructs
• You can control the sequence of execution of statements by using various
control statements and loops.
• Decision Structures
• If-then-Else
• Select Case
• Loop Structures
• While-End While
• Do-Loop
• For-Next
©NIIT Introduction to VB .NET Lesson 1B / Slide 37 of 43
- 38. Working With Windows Forms
Demo
Creating Windows Forms
©NIIT Introduction to VB .NET Lesson 1B / Slide 38 of 43
- 39. Working With Windows Forms
Problem Statement
Create an application to accept a user name and a password through a startup
screen. The application should check whether the login name is “sa” and the
password is “callcenter”. The user should be provided with three logon attempts.
After three unsuccessful logon attempts, application should display an error
message. Also, the stratup screen should be closed.
©NIIT Introduction to VB .NET Lesson 1B / Slide 39 of 43
- 40. Working With Windows Forms
Solution
• Create a new VB .NET Project
• Design a Windows Form
• Add code to validate the user input
• Execute the Application
©NIIT Introduction to VB .NET Lesson 1B / Slide 40 of 43
- 41. Working With Windows Forms
Validating User Inputs
• There are two ways to prompt a user for entering data properly. They are:
• Display an error message to a user using the MessageBox function
• Use the ErrorProvider control
©NIIT Introduction to VB .NET Lesson 1B / Slide 41 of 43
- 42. Working With Windows Forms
Summary
In this lesson, you learned that:
• A form is used to accept input from the user and present information to the
user.
• A Windows Form has many properties, methods and events.
• An event gets generated on performing an action such as clicking the mouse
or pressing a key from the keyboard.
• When an event is raised, the code within the event handler is executed.
• Controls can be added to a form to accept input from the user or display
some information on the form.
• Some commonly used controls are TextBox, Label, CheckBox, RadioButton,
and Button.
• Controls can be added at design time or at runtime.
• Variables are used to store data at run time. You can declare a variable by
using the Dim keyword.
• VB .NET provides the many data types, such as, Byte, Short, Integer, Long,
Single etc.
©NIIT Introduction to VB .NET Lesson 1B / Slide 42 of 43
- 43. Working With Windows Forms
Summary (Contd.)
• The decision structures supported by Visual Basic .NET include:
• If-Then-Else
• Select-Case
• The loop structures supported by Visual Basic .NET are:
• While-End While
• Do-Loop
• For-Next
• The data validation in a windows form can be performed by using
ErrorProvider control.
©NIIT Introduction to VB .NET Lesson 1B / Slide 43 of 43