SlideShare a Scribd company logo
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Working With Windows Forms




                  Demo
         Creating Windows Forms




 ©NIIT       Introduction to VB .NET   Lesson 1B / Slide 38 of 43
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
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
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
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
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

More Related Content

PDF
Primary Key & Foreign Key part10
PPTX
Windows form application - C# Training
PPTX
Windows form application_in_vb(vb.net --3 year)
PPTX
Interface in java ,multiple inheritance in java, interface implementation
PPT
Introduction to visual basic programming
PPT
SQLITE Android
PPTX
Asp.net html server control
PPTX
SQL Functions
Primary Key & Foreign Key part10
Windows form application - C# Training
Windows form application_in_vb(vb.net --3 year)
Interface in java ,multiple inheritance in java, interface implementation
Introduction to visual basic programming
SQLITE Android
Asp.net html server control
SQL Functions

What's hot (20)

PPT
Advanced sql
PPTX
Interface in java
PPTX
introduction to visual basic PPT.pptx
PDF
Memory management
PDF
Compiler design error handling
PPTX
Inheritance in c++
PPTX
Inheritance in java
PPT
Sql ppt
PPTX
Data types
PPT
RichControl in Asp.net
PDF
View & index in SQL
PPTX
Java program structure
PPTX
Windowforms controls c#
PPTX
DBMS Keys
PPTX
Java awt (abstract window toolkit)
PPT
Visual Basic menu
PPTX
sql function(ppt)
PPTX
SQL, Embedded SQL, Dynamic SQL and SQLJ
PPTX
Relational model
Advanced sql
Interface in java
introduction to visual basic PPT.pptx
Memory management
Compiler design error handling
Inheritance in c++
Inheritance in java
Sql ppt
Data types
RichControl in Asp.net
View & index in SQL
Java program structure
Windowforms controls c#
DBMS Keys
Java awt (abstract window toolkit)
Visual Basic menu
sql function(ppt)
SQL, Embedded SQL, Dynamic SQL and SQLJ
Relational model
Ad

Viewers also liked (6)

PDF
Vb tutorial
PPTX
SOAP--Simple Object Access Protocol
PPT
Vb.Net Web Forms
PPTX
Ch2(working with forms)
PPT
Visual basic ppt for tutorials computer
PPT
Server Controls of ASP.Net
Vb tutorial
SOAP--Simple Object Access Protocol
Vb.Net Web Forms
Ch2(working with forms)
Visual basic ppt for tutorials computer
Server Controls of ASP.Net
Ad

Similar to Vb.net session 02 (20)

PPS
Vb.net session 09
PPS
Vb.net session 03
PPTX
UNIT - 1 VISUAL BASIC PRESENTATION FOR IT
PPS
01 gui 01
PPS
Vb.net session 10
PPTX
TVL ICT_Introduction to Visual Basic.pptx
PPS
Vb.net session 01
PPS
Vb.net session 15
PPS
03 gui 04
PDF
Introduction to Visual Basic 6.0
PPT
Ms visual-basic-6
PPTX
Object Oriented Programming I
PPS
08 gui 11
PPS
Vb net xp_02
PPTX
Introduction to Visual Basic 6.0 Fundamentals
PDF
VISUAL PROGRAMMING
PPS
02 gui 02
PDF
vb-160518151614.pdf
PPTX
vb-160518151614.pptx
PPTX
vb.pptx
Vb.net session 09
Vb.net session 03
UNIT - 1 VISUAL BASIC PRESENTATION FOR IT
01 gui 01
Vb.net session 10
TVL ICT_Introduction to Visual Basic.pptx
Vb.net session 01
Vb.net session 15
03 gui 04
Introduction to Visual Basic 6.0
Ms visual-basic-6
Object Oriented Programming I
08 gui 11
Vb net xp_02
Introduction to Visual Basic 6.0 Fundamentals
VISUAL PROGRAMMING
02 gui 02
vb-160518151614.pdf
vb-160518151614.pptx
vb.pptx

More from Niit Care (20)

PPS
Ajs 1 b
PPS
Ajs 4 b
PPS
Ajs 4 a
PPS
Ajs 4 c
PPS
Ajs 3 b
PPS
Ajs 3 a
PPS
Ajs 3 c
PPS
Ajs 2 b
PPS
Ajs 2 a
PPS
Ajs 2 c
PPS
Ajs 1 a
PPS
Ajs 1 c
PPS
Dacj 4 2-c
PPS
Dacj 4 2-b
PPS
Dacj 4 2-a
PPS
Dacj 4 1-c
PPS
Dacj 4 1-b
PPS
Dacj 4 1-a
PPS
Dacj 1-2 b
PPS
Dacj 1-3 c
Ajs 1 b
Ajs 4 b
Ajs 4 a
Ajs 4 c
Ajs 3 b
Ajs 3 a
Ajs 3 c
Ajs 2 b
Ajs 2 a
Ajs 2 c
Ajs 1 a
Ajs 1 c
Dacj 4 2-c
Dacj 4 2-b
Dacj 4 2-a
Dacj 4 1-c
Dacj 4 1-b
Dacj 4 1-a
Dacj 1-2 b
Dacj 1-3 c

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Approach and Philosophy of On baking technology
PPTX
Big Data Technologies - Introduction.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Spectral efficient network and resource selection model in 5G networks
Building Integrated photovoltaic BIPV_UPV.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Review of recent advances in non-invasive hemoglobin estimation
Programs and apps: productivity, graphics, security and other tools
Understanding_Digital_Forensics_Presentation.pptx
Encapsulation theory and applications.pdf
Electronic commerce courselecture one. Pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
20250228 LYD VKU AI Blended-Learning.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
Digital-Transformation-Roadmap-for-Companies.pptx
Approach and Philosophy of On baking technology
Big Data Technologies - Introduction.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectral efficient network and resource selection model in 5G networks

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