Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Programming in Visual Basic

Similar presentations


Presentation on theme: "Fundamentals of Programming in Visual Basic"— Presentation transcript:

1 Fundamentals of Programming in Visual Basic
Chapter 3-A Fundamentals of Programming in Visual Basic Chapter 3 - Visual Basic Schneider hhhhhhh

2 Outline and Objectives
Visual Basic Objects Visual Basic Events Numbers Strings Input/Output Built-In Functions Chapter 3 - Visual Basic Schneider hhhhhhh

3 The Initial Visual Basic Screen
Menu bar Project Explorer window Toolbar Toolbox Properties window Description pane Form Project Container window Form Layout window Chapter 3 - Visual Basic Schneider hhhhhhh

4 Steps to Create a Visual Basic Program
1. Create the interface by placing controls on the form 2. Set properties for the controls and the form 3. Write code for event procedures associated with the controls and the form Chapter 3 - Visual Basic Schneider hhhhhhh

5 Four Useful Visual Basic Controls
Text Boxes Labels Command Buttons Picture Boxes The icons in the Tool Box represent objects that can be placed on the form. The four objects discussed today are: text boxes….. Text Box: you use a text box primarily to get information, referred to as input , from the user Label is placed next to text box to tell the user what type of information to enter into the to he text box Command button : The user clicks a command button to initiate an action Picture Boxes: You use a picture box to display text or graphics Chapter 3 - Visual Basic Schneider hhhhhhh

6 Placing a Text Box on a Form
Double-click on the text box icon in the toolbox to add a text box to your form Activate the properties window (Press F4) Set values of properties for text box Go over resizing and dragging the text box The first line of the properties window (called the object box) reads “Text1 TextBox” . Text1 is the current name of the textbox. The two Tab permit you to view the list of properties either alphabetically or grouped by categories.. We discuss four properties : The Text property determines the words in the text box. Press Shift+Ctrl+F to move to the first property beginning with F . Move to the property ForeColor. The foregroung color is the color of the text.. Click on Palette tab to display a selection of colors. Highlight the Font property and change it to Italic Chapter 3 - Visual Basic Schneider hhhhhhh

7 Placing a label on a Form
Chapter 3 - Visual Basic Schneider hhhhhhh

8 Some Useful Properties of Objects
Name Caption Text (for Text Boxes) BorderStyle Visible BackColor Alignment Font Border Style: Setting the BorderStyle to 0-None removes the border from an object Visible: Setting the property to false hides an object when the program run. The object can be set to reappear with code BackColor: Specifies the background color for text box, label, picture box or form.. Also specific background color for a command button having Style set to “1-Graphical” BackStyle: The background of a label is opaque by default. Setting the background style of a label to transparent causes whatever is behind the label remain visible.; the background color of the label essentially becomes “see through” Font: Two unusual fonts are Symbols and Wingdings> For instance with the windingsfonts , changing the text to % & ‘ and J yields a bell, a book, a candle and a smiling face Chapter 3 - Visual Basic Schneider hhhhhhh

9 Chapter 3 - Visual Basic Schneider
Naming Objects: Use the Property window to change the Name property of an object Good programming practice dictates that each object name begins with a three letter prefix that identifies the type of object. Chapter 3 - Visual Basic Schneider hhhhhhh

10 Chapter 3 - Visual Basic Schneider
Naming Objects: Chapter 3 - Visual Basic Schneider hhhhhhh

11 Chapter 3 - Visual Basic Schneider
Naming Objects An Object Name Must Start with a letter Can include numbers and underscore (_) Cannot include punctuation or spaces Can be a maximum of 40 characters Chapter 3 - Visual Basic Schneider

12 Chapter 3 - Visual Basic Schneider
Visual Basic Events Code is a set of statements that instruct the computer to carry out a task. Code can be associated with events When an event occurs, the code associated with that event (called an Event Procedure) is executed. When a VB program is run, a form and its controls appear on the screen. Normally, nothing happens until the user takes an action, such as clicking a control or pressing the Tab key. Such an action is called event. Chapter 3 - Visual Basic Schneider hhhhhhh

13 Creating An Event Procedure
Double-click on an object to open a Code window. (The empty default event procedure will appear. Click on the Procedure box if you want to display a different event procedure.) Write the code for that event procedure. Object Property Setting frmWalkthrough Caption Demonstration txtPhrase Text (blank) cmdBold Caption Make Phrase Bold Object Box Procedure Box: Contains a list of all possible event procedures associated with the text box Choose txtPhrase_LostFocus() txtPhrase.Font.Size = 12 End Sub Choose GotFocus and type: txtPhrase.Font.Size = 8 txtPhrase.Font.Bold = False Go to command Button and choose cmdBold_Click and type txtPhrase.Font.Bold = True Chapter 3 - Visual Basic Schneider hhhhhhh

14 Example of An Event Procedure
Private Sub objectName_event ( ) statements End Sub Private Sub Text2_GotFocus( ) Text2.Font.Size = 12 Text2.Font.Bold = False Text2.forecolor=vbRed The word Sub signals the beginning of the event procedure, and also identifies the Object and the event occurring to that object The word private indicates that the event procedure cannot be invoked by an event from another procedure. The word Sub means subprogram Example: the event procedure private Sub cmdButton_Click () txtBox.Text = “” End Sub The event clicking cmdButton is different from the event clicking picBox_Click Chapter 3 - Visual Basic Schneider hhhhhhh

15 Chapter 3 - Visual Basic Schneider
More Examples Private Sub cmdButton_Click( ) txtBox.ForeColor = vbRed txtBox.Font.Size = 24 txtBox.Text = “Hello” End Sub Each color can be identified by a sequence of digits and letter beginning with &H. &HFF& = Red &HFF00& = Green &HFF000& = Blue Chapter 3 - Visual Basic Schneider hhhhhhh

16 Exercises: What is wrong??!
A. Private Sub cmdButton_Click( ) frmHi= “Hello” End Sub B. Private Sub cmdButton_Click( ) txtOne.ForeColor= “red” Chapter 3 - Visual Basic Schneider

17 Exercises: What is wrong??!
C. Private Sub cmdButton_Click( ) txtBox.Caption = “Hello” End Sub D. Private Sub cmdButton_Click( ) txtOne.MultiLine= True Chapter 3 - Visual Basic Schneider

18 Chapter 3 - Visual Basic Schneider
Tips Most Properties can be set or altered at run time with code. cmdButton.visible = False The MultiLine property of a text box can only be set from the properties window, also the Name property…. “” surrounds Caption, Name, Font.Name or strings. Not True for vars or numeric constants Chapter 3 - Visual Basic Schneider

19 Chapter 3 - Visual Basic Schneider
Color Constants At design time colors are selected from a palette At run time the eight most common colors can be assigned with the color constants: vbBlack vbMagenta vbRed vbCyan vbGreen vbYellow vbBlue vbWhite Chapter 3 - Visual Basic Schneider

20 Components of Visual Basic Statements
Constants Variables Keywords (reserved words) Chapter 3 - Visual Basic Schneider hhhhhhh

21 Chapter 3 - Visual Basic Schneider
Constant Can NOT change during the execution of a program. Types of Constants: numeric constants string constants Chapter 3 - Visual Basic Schneider hhhhhhh

22 Valid Numeric Constants:
Integer Real number Chapter 3 - Visual Basic Schneider hhhhhhh

23 Invalid Numeric Constants:
14, % $190.04 & Chapter 3 - Visual Basic Schneider hhhhhhh

24 Arithmetic Operations
Operator Operation Basic expression ^ Exponentiation A ^ B * Multiplication A * B / Division A / B Addition A + B Subtraction A - B Chapter 3 - Visual Basic Schneider hhhhhhh

25 Chapter 3 - Visual Basic Schneider
Scientific Notation 10n= 103=1000 10-n= 10-3=0.0001 b.10±r VB Notation bE±r 1.4 * 10-45 14 * 10 ^-45 1.4E -45 1.4E -44 Chapter 3 - Visual Basic Schneider

26 Chapter 3 - Visual Basic Schneider
String Constants: A sequence of characters treated as a single item The characters in a string must be surrounded by double quotes (“ ”) Chapter 3 - Visual Basic Schneider hhhhhhh 26

27 Valid String Constants
“A rose by any other name” “9W” “134.23” “She said, ‘stop , thief!’ ” Chapter 3 - Visual Basic Schneider hhhhhhh 27

28 Invalid String Constants
‘Down by the Seashore’ “134.24 “She said, “Stop, thief!”” Chapter 3 - Visual Basic Schneider hhhhhhh 28

29 Chapter 3 - Visual Basic Schneider
Variables A storage location in main memory whose value can be changed during program execution. These storage locations can be referred to by their names. Every variable has three properties: a Name, a Value, and a Data Type. Types of variables: Numeric and String Chapter 3 - Visual Basic Schneider hhhhhhh

30 Rules for Naming Variables
Must begin with a letter Must contain only letters, numeric digits, and underscores ( _ ) Can have up to 255 characters VB is insensitive for uppercase or lowercase Cannot be a Visual Basic language keyword (for example, Sub, End, False) VB does not distinguish between uppercase and lowercase letters Example: numberOfCars, tax_Rate_1994 Let statement assigns values to variables and Print method displays the values of variable Chapter 3 - Visual Basic Schneider hhhhhhh

31 Chapter 3 - Visual Basic Schneider
Valid Variable Names: timeElapsed a1b2c3 Var_1 n celsius Chapter 3 - Visual Basic Schneider hhhhhhh 31

32 Invalid Variable Names:
maximum/average 1stChoice square yard Name? Chapter 3 - Visual Basic Schneider hhhhhhh 32

33 Chapter 3 - Visual Basic Schneider
Keywords Words that have predefined meaning to Visual Basic . Can Not be used as variable names. Examples: End - Print Sub - Let If -Select While -Call The VB editor automatically capitalizes the first letter of reserved word Chapter 3 - Visual Basic Schneider hhhhhhh

34 Chapter 3 - Visual Basic Schneider
Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable must be on the left and the expression on the right. Chapter 3 - Visual Basic Schneider hhhhhhh

35 Assignment Statement:
The statement var = expr assigns the value of the expression to the variable tax = 0.02 * (income * dependents) sum = 2 + x y Chapter 3 - Visual Basic Schneider hhhhhhh

36 Valid Assignment Statements
count = count + 1 num = 5 count = count + num /2 Chapter 3 - Visual Basic Schneider hhhhhhh

37 Invalid Assignment Statements
10 = count count + 1 = count Chapter 3 - Visual Basic Schneider hhhhhhh

38 Chapter 3 - Visual Basic Schneider
String Variables A string variable stores a string. The rules for naming string variables are identical to those for naming numeric variables. When a string variable is first declared, its value is the empty string. The value of string variable is assigned or altered with Let statements and displayed with Print methods just like the Chapter 3 - Visual Basic Schneider hhhhhhh 38

39 Visual Basic Print Statement
Print is a method used to display data on the screen or printer. Can be used to display values of variables or expressions Chapter 3 - Visual Basic Schneider hhhhhhh

40 Examples of Print Statements
Private Sub cmdCompute_Click() picResults.Print 3 + 2 picResults.Print 3 - 2 picResults.Print “3 * 2” picResults.Print 3 / 2 picResults.Print 3 ^ 2 picResults.Print 2 * (3 + 4) End Sub Chapter 3 - Visual Basic Schneider hhhhhhh

41 Examples of Print Statements
speed=3 taxRate=speed+5 total=30 picOutput.Print speed picOutput.Print taxRate picOutput.Print “Class average is”; total/3 3 8 Class average is 10 Chapter 3 - Visual Basic Schneider hhhhhhh

42 Examples of Print Statements
y = 5 picOutput.Print (x + y) / 2, x / y Output: 10 3 Chapter 3 - Visual Basic Schneider hhhhhhh

43 String Variable Example
Private Sub cmdShow_Click() picOutput.Cls phrase = "win or lose that counts." picOutput.Print "It's not whether you "; phrase picOutput.Print "It's whether I "; phrase End Sub Chapter 3 - Visual Basic Schneider hhhhhhh

44 Chapter 3 - Visual Basic Schneider
Concatenation Two strings can be combined by using the concatenation operation. The concatenation operator is the ampersand (&) sign. Chapter 3 - Visual Basic Schneider hhhhhhh

45 Example &: is always used to make concatenation
+: makes concatenation when it is used with strings, and as summation with numbers What about “hi”+5 ???

46 Examples of Concatenation:
strVar1 = “Hello” strVar2 = “World” picOutput.Print strVar1& strVar2 txtBox.Text = “32” & Chr(176) & “ Fahrenheit” Chapter 3 - Visual Basic Schneider hhhhhhh

47 Chr() & Asc() Asc(A) = 65 Asc(B) = 66 Asc(a) = 97 Asc( ) = 176
The characters have numbers associated with them, these values called ANSI values of characters Chr(65) = A Chr(66) = B Chr(97) = a Chr(176) = Asc(A) = 65 Asc(B) = 66 Asc(a) = 97 Asc( ) = 176

48 48

49 Val, Str 23 10 50 Val (“23”) = 23 Val (23) =23 Str (23) =
Val(“Hi”) = 0 Val(“89Hello”) = 89 Val(“89Hi10”) = 89 Str("10") + Str("50") = Str(“Hello”)  error 23 space

50 Declaring Variable Types
Use the Dim statement to declare the type of a variable. Examples: Dim number As Integer Dim flower As String Dim interestRate As Single From now on we will declare all variables. Declaring variables is regarded as good programming practice. Chapter 3 - Visual Basic Schneider hhhhhhh

51 Chapter 3 - Visual Basic Schneider
Data Types Single: a numeric variable that stores real numbers (0 By default) Integer: a numeric variable that stores integer numbers (from to 32767) (0 By default) String: a variable that stores a sequence of characters (“”empty string By default) Boolean: a variable that stores True or False (False By default) The default data type is a single precision numeric variable Therefore Number and Number! Are the same Double precision variable is used to store number with many digits A single precision variable is accurate to about seven decimal points where double precision is accurate to about 15 decimal points Is used when a high degree of accuracy is needed. If you try to assign a real number , the number would be cut off and an integer would be assigned to it. Declaring variables is regarded as good programming practice Example: Dim variableName As String Dim variableName As Single Type Declaration Tags Chapter 3 - Visual Basic Schneider hhhhhhh

52 Using Text Boxes for Input/Output
The contents of a text box are always a string type. Numbers can be stored in text boxes as strings. Chapter 3 - Visual Basic Schneider hhhhhhh

53 Using Text Boxes for Input/Output
The contents of a text box should be converted to a number before being assigned to a numeric variable. Val(txtBox.Text) gives the value of a numeric string as a number Example: Dim numVar as Single numVar = Val(txtBox.Text) Function Input Output Str number string Val string number If str is a string representation of a number, then Val(str) is that number. Conversely, if num is a number, then Str(num) is a string representation of the number. Therefore statements such as numVar = Val(txtBox.Text) and txtBox.Text = Str(numVar) Chapter 3 - Visual Basic Schneider hhhhhhh

54 The contents of a text box is always a string
Numbers types into the text box are stored as strings. Therefore, they should be converted to numbers before being assigned to numeric.

55 Chapter 3 - Visual Basic Schneider
Example Example 1 xString=“528” xValue=Val(xString)  xValue=528 Example 2 yValue=428 yString=Str(yValue) yString=“428” xString xValue “528” 528 428 “428” xValue xString Chapter 3 - Visual Basic Schneider hhhhhhh

56 Program Documentation
An apostrophe (') is used to indicate that the remainder of the line is a comment. (Comments are ignored by Visual Basic.) Remarks can appear on a separate line or following a Visual Basic statement. Chapter 3 - Visual Basic Schneider hhhhhhh


Download ppt "Fundamentals of Programming in Visual Basic"

Similar presentations


Ads by Google