Introduction to Visual Basic.net 3

27
INTRODUCTION TO VISUAL BASIC.NET F 5227 VISUAL BASIC.NET PROGRAMMING 1

description

Visual Basic.Net Note for Beginners

Transcript of Introduction to Visual Basic.net 3

Page 1: Introduction to Visual Basic.net 3

INTRODUCTION TO VISUAL BASIC.NET

F 5227 VISUAL BASIC.NET PROGRAMMING 1

Page 2: Introduction to Visual Basic.net 3

2

Learning ObjectivesBy the end of the class, students should be able to :

• Describe the various file that make up a VB project.• Differentiate time in VB.• Use Help menu in VB.• Define objects, properties and methods.• Identify the use of Visual Basic objects controls.• Perform the steps to place and size object controls.• Identify the various properties of object controls.• Create suitable naming prefixes for the object controls.

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 3: Introduction to Visual Basic.net 3

Visual Basic project consists of at least two (2) , and usually more files.

3

File Types Using

.sln Solution file, a text file that holds information about the solution and project it contains

.suo Solution user option file, stores info about the state of the integrated development environment (IDE) so that all information can be stored at time you open the solution

.vb A text file that holds the code procedure that you write

.resx A resource file for the form. A text file that defines all resources used by the form

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 4: Introduction to Visual Basic.net 3

In Visual Basic, you will work with objects, which have properties and methods.

4

Types Meaning Examples

Object Thing (noun). Forms and controls (text boxes, command button)

Properties Tell something about an object (adjectives).

Name, color, size

Methods Action associated with objects (verb).

Move, Print, Resized

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 5: Introduction to Visual Basic.net 3

Property is attribute of a control object that can be set by the designer

Name property: identifies the control Should start with three letter prefix▪ lbl for label▪ txt for text box▪ frm for form▪ cmd for command button

Must start with a letter Maximum of 40 character Can include letters, numbers, and underscore (_) Can not have space.

5F 5227 VISUAL BASIC.NET PROGRAMMING

Page 6: Introduction to Visual Basic.net 3

Caption property for labels: Allows you to change the text displayed

on a command buttonStretch Property:

Enlarges the image control and the image simultaneously (if set to True)

Use ctrl + shift + property’s first letter to find the next property.

6F 5227 VISUAL BASIC.NET PROGRAMMING

Page 7: Introduction to Visual Basic.net 3

Control: An object with built-in properties that

can be placed on a VB form.Single Click and draw on form.Double-click to add control to center

of form : Can be moved around from there Use sizing handles to change size

7F 5227 VISUAL BASIC.NET PROGRAMMING

Page 8: Introduction to Visual Basic.net 3

The VB menu system provides access to all commands.

The Toolbar uses icons for more commonly used menu commands.

8F 5227 VISUAL BASIC.NET PROGRAMMING

Page 9: Introduction to Visual Basic.net 3

9

Lets start explorer your toolbar controls!!

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 10: Introduction to Visual Basic.net 3

10

Explore the toolbar controls.  Hover your mouse over each tool and you will see a tooltip appear with the name of the control. If you double click a control, it will appear on the form.  Draw the following controls on your form:-

3 textboxes

4 labels

1 command button

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 11: Introduction to Visual Basic.net 3

11

Move the controls so that your form looks similar to the picture below:-

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 12: Introduction to Visual Basic.net 3

12

1. Save your form by by selecting Form1 in the Project Explorer window.  Right-click the mouse and click Save Form.  A dialog box will appear... 

• type in a name for your form file, such as calculatorForm.• create a folder to save the form file in.• click Save.

2. Save your project by by selecting Project1 in the Project Explorer window.  Right-click the mouse and click Save

project.  A dialog box will appear... 

• type in a name for your project, such as calculator.• create a folder to save the project file in.• click Save.

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 13: Introduction to Visual Basic.net 3

13

Used to change the properties of controls at design time.

Class of selected objectProperties List Tabs

Properties Window

Scrollable list of properties

Description Pane

Name of selected object

Object box

Setting box

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 14: Introduction to Visual Basic.net 3

14

Now you need to change the properties of the controls you drew in Activity A, so that your form looks similar to the one shown below:-

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 15: Introduction to Visual Basic.net 3

15

1. Select  Label1  and in the property window, change :- Name from Label1 to lblName1.- Caption from Label1  to Simple Calculator.

2. Select each of the other labels in turn and change their Captions to:- 

First Number Second Number Result

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 16: Introduction to Visual Basic.net 3

16

3. Select Text1 and in the property window, change :- Name from Text1 to txtNumber1.

4. Select each of textboxes and delete Text1 etc. from the Text property.   Notice what happens to the text inside the textboxes.

5. Continue same steps for each textboxes.

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 17: Introduction to Visual Basic.net 3

17

6. Select Command1 and in the property window, change :- Name from Command1 to cmdPress.

7. Select the Command Button and change the Caption property value

from CommandButton1 to Add. Notice what happens to the text displayed on the button.

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 18: Introduction to Visual Basic.net 3

18

8. Select the lblName1 again.  9. In the property window, select the Font property. Click the little button at the right side of the row. The Font dialog should appear. Change the font size to 10pt.  Notice what happens to the text displayed in the label.  It should be bigger..

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 19: Introduction to Visual Basic.net 3

19

Don't forget to save your project file and form file!!!

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 20: Introduction to Visual Basic.net 3

20

•To execute a program in VB, use one of these three (3) methods : •Open the Run menu and choose Start.  •Press F5, the short cut key for the select Start command.•Press Start button on the toolbar.

• To stop your program running, click on the End button on the main horizontal toolbar near the top of the VB window, the little square.• Alternatively, select End from the Run menu.

End

Break

Start

F 5227 VISUAL BASIC.NET PROGRAMMING

Page 21: Introduction to Visual Basic.net 3

F 3005 VISUAL BASIC PROGRAMMING 21

1. Run your Calculator program.

Your program doesn't do anything yet, does it? 2. Stop your Calculator program

Page 22: Introduction to Visual Basic.net 3

F 3005 VISUAL BASIC PROGRAMMING 22

Visual Basic is an event driven language.

Waits for events - such as user interaction to occur.

Once your form is created and the properties are set, the next step is to add code to make your program respond to events. 

Page 23: Introduction to Visual Basic.net 3

F 3005 VISUAL BASIC PROGRAMMING 23

In the calculator program, the only event we need to add code for is the user clicking the Add button.

When this happens, we need the program to:-

Add together the two numbers the user typed in the first two textboxes Display the result in the third textbox.

Page 24: Introduction to Visual Basic.net 3

F 3005 VISUAL BASIC PROGRAMMING 24

1. Type the code shown below into your Calculator code window

2. Note: You may have problems if you have changed the Name of any of your textboxes.  If so, rename them back to Text1, Text2 and so on.

3. Make sure your code is typed in exactly the same as shown above.  When you are satisfied -  Save everything.

4. Now run your program.

Page 25: Introduction to Visual Basic.net 3

F 3005 VISUAL BASIC PROGRAMMING 25

Page 26: Introduction to Visual Basic.net 3

You are in “Design Time” when you are creating and modifying the project and adding code.

You are in “Run Time” when you click Start icon and the VB program is being executed.

You can stop the project by clicking the “Break Mode” Icon.

You can stop the project and exit Run time by clicking the End Icon.

F 3005 VISUAL BASIC PROGRAMMING 26

Page 27: Introduction to Visual Basic.net 3

There are three types of Help: Help menu option▪ Contents▪ Index▪ Search

context-sensitive help▪ pressing the F1 key provides help on whatever

item the cursor is located. and Auto Help▪ VB tries to help you with a code statement by

providing:▪ A list of items to complete the statement▪ Info on the statement you have started▪ Tips on the type of data you are working with

F 3005 VISUAL BASIC PROGRAMMING 27