vb .net tutorial - 2

32
Creating Windows Forms ©NIIT Creating Windows Forms/Lesson 2/Slide 1 of 30 Objectives In this lesson, you will learn to: Identify the features of Windows Forms Identify various language features required to work with Windows Forms Identify various Windows Forms controls Create Windows Forms

Upload: mathes99994840202

Post on 15-Nov-2014

42 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 1 of 30

Objectives

In this lesson, you will learn to:

Identify the features of Windows Forms

Identify various language features required to work with Windows Forms

Identify various Windows Forms controls

Create Windows Forms

Page 2: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 2 of 30

Understanding the User Interface

A user interface is the means by which a user interacts with an application. A well-designed user interface is an effective means of making an application user-friendly.

Types of User Interfaces

Character user interface (CUI)

Graphical user interface (GUI)

Page 3: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 3 of 30

Windows Form

Is a representation of any window displayed in an application.

Is used to accept input from a user and display information.

Page 4: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 4 of 30

Windows Forms (Contd.)

Windows Form properties:

Are used to determine the appearance of a Windows Form at run time.

Some commonly used properties are:

Name

BackColor

BackgroundImage

Font

Size

StartPosition

Text

WindowState

Page 5: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 5 of 30

Windows Forms (Contd.)

Windows Forms events:

Click

Closed

Deactivate

Load

MouseMove

MouseDown

MouseUp

Page 6: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 6 of 30

Windows Forms (Contd.)

Windows Forms methods:

Show()

Activate()

Close()

SetDesktopLocation()

Page 7: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 7 of 30

Visual Basic.NET Language Features

Data types:

Byte

Short

Integer

Long

Single

Double

Decimal

Boolean

Page 8: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 8 of 30

Visual Basic.NET Language Features (Contd.)

Data types: (Contd.)

Char

String

Date

Object

Page 9: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 9 of 30

Visual Basic .NET Language Features (Contd.)

Variables

Allow you to store data.

Have a data type and a name.

Can be declared by using the Dim statement.

Arrays

Are collections of values of the same data type.

Contain elements that can be accessed using a single name and an index number representing the position of the element within the array.

Page 10: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 10 of 30

Visual Basic .NET Language Features (Contd.)

Are declared using the following syntax:

Dim arrayname(Number-of-elements) as datatype

Can be resized using the Redim statement.

Operators

Are used to process the data entered by a user.

Can be categorized as follows:

Arithmetic operators such as +, -, *, /, \, and Mod

Comparison operators such as =, <, >, <=, >=, and <>

Logical operators such as And, Or, Not, and Xor

Concatenation operators such as & and +

Page 11: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 11 of 30

Visual Basic .NET Language Features (Contd.)

Control flow constructs

Decision structures

If-then-else

Select case

Loop structures

While-End while

Do-Loop

For-Next

Nested control statements

Page 12: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 12 of 30

Just a Minute…

1. Write a loop structure to display all odd numbers between 1 and 100.

2. Write the construct to check whether the character stored in the variable X is a vowel and display an appropriate message.

Page 13: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 13 of 30

Problem Statement 2.D.1

For the Call Center application, a startup screen needs to be provided to accept the user name and password. The application should allow the user to enter the user name and password for a maximum of three times. If in all of the three attempts, a wrong user name and password is entered, the application should display an error and close.

Page 14: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 14 of 30

Task List

Identify the variables required to store the values in the application.

Identify the controls to be used for accepting input from the user.

Identify the mechanism to validate the user input.

Create a Windows Form.

Add controls to the Windows Form.

Write the code to validate the user input.

Execute the application.

Page 15: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 15 of 30

Task 1: Identify the variables required to store the values in the application.

Result:

The variables required for accepting the login details from a user are:

Counter

Login Name

Password

Page 16: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 16 of 30

Task 2: Identify the controls to be used for accepting input from the user.

Visual Basic .NET provides a number of controls that can be added to a form:

TextBox

Is used to display text to a user or accept input from a user.

Has the following properties:

Text

Multiline

Passwordchar

Page 17: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 17 of 30

Task 2: Identify the controls to be used for accepting input from the user. (Contd.)

Label

Is used to provide information or description of another control on the Windows Form.

Has the following properties: Text AutoSize

Page 18: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 18 of 30

Task 2: Identify the controls to be used for accepting input from the user. (Contd.)

LinkLabelIs used to display the text as a link. Has the following properties:

LinkColor ActiveLinkColor DisabledLinkColor LinkVisited

Page 19: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 19 of 30

Task 2: Identify the controls to be used for accepting input from the user. (Contd.)

ListBox

Is used to display a list of items to a user.

Can be populated by using the Add() method of the Items collection.

Has the following properties:

SelectionMode

Sorted

SelectedIndex

SelectedItem

Page 20: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 20 of 30

Task 2: Identify the controls to be used for accepting input from the user. (Contd.)

ComboBox

Is used to display a drop-down list of items.

Can be populated by using the Add() method of the Items collection.

Has the following properties:

Text

Sorted

SelectedIndex

SelectedItem

Page 21: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 21 of 30

Task 2: Identify the controls to be used for accepting input from the user. (Contd.)

CheckBox

Is used to set Yes/No or True/false options.

Has the following properties:

Text

Checked

Page 22: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 22 of 30

Task 2: Identify the controls to be used for accepting input from the user. (Contd.)

RadioButton

Is used to provide a set of mutually exclusive options to the user.

Has the following properties:

Text

Checked

Page 23: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 23 of 30

Task 2: Identify the controls to be used for accepting input from the user. (Contd.)

GroupBox

Is used to group related controls.

Button

Is used to perform an action when a user clicks it.

Has the Text property, which is used to set the text displayed on the button.

StatusBar

Is used to display the status information.

Has the following properties:

ShowPanels

Text

Page 24: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 24 of 30

Task 2: Identify the controls to be used for accepting input from the user. (Contd.)

Some common properties found in most of the controls are:

Name

Visible

Location

Enabled

Page 25: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 25 of 30

Task 2: Identify the controls to be used for accepting input from the user. (Contd.)

Some common events found in most of the controls are:

KeyDown KeyUp KeyPress MouseDown MouseUp MouseMove ReSize VisibleChanged

Page 26: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 26 of 30

Task 2: Identify the controls to be used for accepting input from the user. (Contd.)

Result:

You will use the following controls for the given problem statement:

Label

TextBox

Button

Page 27: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 27 of 30

Task 3: Identify the mechanism to validate the user input.

Result:

The program should use the If-Then construct to validate the user name and the password. The If‑Then construct can also be used to ensure that value of the counter is not greater than 3.

Page 28: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 28 of 30

Task 4: Create a Windows Form.

Task 5: Add controls to the Windows Form.

Task 6: Write the code to validate the user input.

Task 7: Execute the application.

Page 29: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 29 of 30

Just a Minute…

You have created two forms named Form1 and Form2. Write the code so that when a user clicks the OK button in Form1, Form2 should be displayed.

Page 30: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 30 of 30

Problem Statement 2.P.1

Diaz Telecommunications needs an application to accept order details. The details to be accepted include order number, date, customer ID, product ID, cost, and advance. The product IDs should be displayed in a combo box. When a user clicks the Save button after entering order details, the application should verify that none of the fields is left blank and display an appropriate message.

Page 31: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 31 of 30

Summary

In this lesson, you learned that:

A form is used to accept input from the user and present information to the user.

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.

Variables are used to store data at run time. You can declare a variable by using the Dim statement.

The decision structures supported by Visual Basic .NET include:  If-Then-Else

Select-Case

   

Page 32: VB .net tutorial - 2

Creating Windows Forms

©NIIT Creating Windows Forms/Lesson 2/Slide 32 of 30

Summary (Contd.)

The loop structures supported by Visual Basic .NET are:

While-End While

Do-Loop

For-Next

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.