Introduction to Programming Fundamentals of Programming in Visual

























- Slides: 25

Introduction to Programming Fundamentals of Programming in Visual Basic

Outline and Objective n n Visual Basic Objects Visual Basic Events n n Numbers Strings Input/Output Built-In Functions

The Initial Visual Basic screen Toolbar Menu bar Project Explorer window Toolbox Form Properties window

Steps to Create a Visual Basic Program 1. Create the Objects 2. Set Properties 3. Write the Code for each Event

Four most useful Visual Basic Controls n n Text Boxes Labels Command Buttons Picture Boxes

A Text Box Walkthrough: n n n Double-click on Text Box to add a Text Box to your form Activate the Properties window (Press F 4) Set values of Properties for Text Box

A Text Box Walkthrough Text box

Some Useful Properties: n n Name Caption n n Border style Visible Back Color Alignment Font

Naming Objects: n n Use the Property window to change the Name property of an object Good Programming habit is that each name begins with three letter prefix that identifies the type of control.

Naming Objects:

Visual Basic Events n n Code is a set of statements that will be executed when you run a program. Write Code for each Event. Most Events are associated with Objects. The code for each event is called an “Event Procedure”.

The steps for creating a VB program: n n n Create the Interface. Set Properties for the objects. Write the code that executes when event occur.

An Event Procedure Walkthrough n n n Create the interface. Set Properties. Double click on the object to open the Code window. Click on the Procedure box to find the event Write the code for that event.

Arithmetic Operations & Hierarchy of Operations Operator ^ * / + - Operation Basic expression Exponentiation A^B Multiplication A*B Division A/B Addition A+B Subtraction A-B

Keywords n n Words that have predefined meaning to Visual Basic. Can Not be used as variable names. Example: Print Cls If While

Internal Documentation n An apostrophe (‘) can be used to indicate comments; comments are ignored by Visual Basic. The keyword Rem can also be used instead of an apostrophe for comments. Remarks can also be placed after program statement too.

Data Types n Each variable in the program is assigned to a data type.

Declaring Variable Types n Use the Dim statement to Declare the type of a variable. Example: Dim number As Integer Dim flower As String Dim interest. Rate As Single

Data Types : n n Single-precision numeric variable: Stores real numbers Double-precision numeric variable: Stores real numbers with many digits Integer: Stores integers Long integer: Stores integers with many digits

Using Text Boxes for Input/Output n n The contents of a text box are always a string. Numbers are also stored in text boxes as strings.

Using Text Boxes for Input/Output n n Therefore, the contents of a text box should be changed to a number before being assigned to a numeric variable. Val (txt. Box. Text) changes the input string into a number. Example: num. Var = Val (txt. Box. Text)

Example (convert miles to furlong and vice versa) Private Sub txt. Furlong_Lost. Focus() txt. Mile. Text = Str(Val(txt. Furlong. Text / 8)) End Sub Private Sub txt. Mile_Lost. Focus() txt. Furlong. Text = Str(8 * Val(txt. Mile. Text)) End Sub

Using Message Box for Output: n n Use message box to get the user’s attention. Message box is a predefined dialog box too.

Syntax for Message Box n Msg. Box prompt, , title

Example of Message Box Msg. Box “Nice try, but no cigar”, , “Consolation” Stays on the screen until the user presses OK