1 / 25

CVEV 118/698 Visual Basic

Learn about control flow statements (If...Then...Else, Select Case) and loop statements (Do...Loop, For...Next, While...Wend) in Visual Basic programming language.

kmcelhannon
Download Presentation

CVEV 118/698 Visual Basic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CVEV 118/698 Visual Basic Lecture 2 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar

  2. Control Flow Statements • An important feature in programming languages is the ability to examine external conditions and act accordingly. • VB provides three control flow (or decision) statements: • If … Then … End If • If … Then … Else … End If (Elseif) • Select Case

  3. If/Then Statements • Example 1: If intAnswer = 1 Then strMood = “happy” End If • Example 2: If intAnswer = 1 Then strMood = “happy” Else strMood = “sad” End If • Example 3: If intAnswer = 1 Then strMood = “happy” Elseif intAnswer = 2 Then strMood = “sad” Else strMood = “don’t care” End If

  4. Select Case Statement • Efficient method for multi-conditional cases. • Select Case WeekDay(intDate) • Case 1 • strDayName = “Monday” • strMessage = “Have a nice week” • Case 6 • strDayName = “Saturday” • strMessage = “Have a nice weekend” • Case 7 • strDayName = “Sunday” • strMessage = “Working on Sunday!!??” • Case Else • strMessage = “Welcome back” • End Select Select Case expression Case Value_1 Statement block_1 Case Value_2 Statement block_2 … Case Value_n Statement block_n Case Else Statement block End Select

  5. Select Case (cont’d) • The Select Case structure compares the results of one expression to several values, and if it matches one of them, the corresponding block of statement is executed. • If more than one Case value matches the expression, only the statement block associated with the first matching Case executes. • Disadvantage: The Select Case evaluates the expression before entering the Case statements, while the If/Then/Else can evaluate a different expression for each ElseIf statement.

  6. Select Case (cont’d) • Example 2 Select Case WeekDay(intDate) Case 1, 2, 3, 4, 5 strDayType = “Workday” strMessage = “Enjoy Work” Case 6, 7 strDayType = “Holiday” strMessage = “Enjoy your weekend” Case Else strDayType = “VBday” strMessage = “Goto your assignment!” End Select

  7. Loop Statements • Loop Statements allow to execute code repetitively. • VB provides three loop statements: • Do … Loop • For … Next • While … Wend

  8. Do … Loop • Syntax 1: Do While condition statement block Loop • VB evaluates the condition, if it is False the block is never executed. If it is True, VB executes the statement block, re-evaluates the condition, and repeats the statement block if it is True. • Example 1: Do While dblGrade <= 80 dblGrade = dblGrade + 1 MsgBox “Work Harder!” Loop

  9. Do … Loop (cont’d) • Syntax 2: Do statement block Loop While condition • The main difference with syntax 1 is that the statement block is executed the first time independently of the condition. The evaluation starts before the second loop. • Example 2: Do dblGrade = dblGrade + 1 MsgBox “Work Harder!” Loop While dblGrade <= 80

  10. For … Next • The loop executes depending on a preset number of times not on a conditional statement. • It uses a variable (counter) that increases or decreases in value each time the statement is executed. • Syntax: For counter = start To end [Step increment] statements Next [counter]

  11. For … Next (cont’d) • Example 1: For intNum = 1 to 10 intSquareNum(intNum) = intNum * intNum Next intNum • Unless specified, the incremental step of the counter (I.e. intNum) is 1 by default. • Example 2: For intNum = 10 to 1 Step -2 intSquareNum(intNum) = intNum * intNum Next intNum

  12. While … Wend • While…Wend executes a block of statements while a condition is true. • Syntax: While condition statement block Wend • Why all this trouble? Just use the Do While Loop 

  13. Functions & Subroutines • VB code is not a monolithic listing. An application in VB is made up of small self contained segments that are “event handler”. • Difference between a function & a subroutine: • A Function executes a series of commands and returns a value. • A Subroutine can be considered a special function since it does not return anything.

  14. Subroutines • A subroutine is a block of statements that carry out a well-defined task. Sub MyFirstSub () …… …… End Sub • All the event procedures in VB are coded as subroutines e.g.: the “click” event of a button.

  15. Functions • A function returns a result, accordingly it must have a type: Function MyFirstFunction () As Double …… …… End Function

  16. Arguments • Arguments are values passed to a procedure (a function or subroutine) and on which the procedure acts. • Example: Function dblCircleArea (r As Double) As Double dblCircleArea = pi * r * r End Function To call the above function: dblCArea1 = dblCircleArea (dblRadius) (where dblCArea1 & dblRadius are previously defined variables of type double)

  17. Arguments (cont’d) • The number of arguments passed must be the same as the number of arguments defined in the function or subroutine. • Example: Function dblRectArea (a, b As Double) As Double dblRectArea = a * b End Function ‘Error will be generated in the following code: dblAreaWrong = dblRectArea(dblLength) ‘The right piece of code is: dblAreaRight = dblRectArea(dblLength,dblWidth)

  18. Optional Arguments • You may need a function to handle in some cases one argument; in other cases two or more. • Then you need to use the “Optional argument”: Function dblRectArea (a As Double,Optional b As Double) As Double dblRectArea = a * a If IsMissing(b) Then Exit Function dblRectAtea = a * b End Function Both calls for the above function are valid: RArea1 = dblRectArea(dblLength) RArea2 = dblRectArea(dblLength,dblWidth)

  19. Forms • A form is the “container” which includes all the “controls” that make up the user interface. • Forms have a built-in functionality that is always available without any coding from you (title bar, resize, etc.). • Properties of a form exist for you to customize its appearance. Some of these properties are: • MinButton, MaxButton • BorderStyle • ControlMenu

  20. Forms (cont’d) Title Bar Minimize Maximize Control Menu Close

  21. Basic Controls • Controls are another key factors in the “event-driven” programming. • VB has a multitude of controls, and many third party companies are developing more. • You can create your own control.

  22. Frames and Option Boxes • Rectangular entity inside a form that groups one or more controls. • Frames are used for esthetics reasons and/or for proper functioning of option boxes: inside each frame, only one option box can be ticked

  23. Frames and Option Boxes: Example

  24. TextBox Properties • Multiline: set to true if you want to display correctly multiple lines. • PasswordChar: set to a character that gets displayed to hide the real characters. • ToolTipText: Yellowish text box that explains about a control. • ScrollBars: Controls the attachment of scroll bars, if the text exceeds the control dimensions. • MaxLength: Maximum number of characters contained. • TabStop: Controls the access to the control with the tab key. • Enabled: Controls the access to the control

  25. What’s Next • Form manipulation • Menu design • Other controls

More Related