Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2002 Prentice Hall. All rights reserved. 1 Outline 3.2Simple Program: Printing a Line of Text 3.3Another Simple Program: Adding Integers 3.7Using a Dialog.

Similar presentations


Presentation on theme: " 2002 Prentice Hall. All rights reserved. 1 Outline 3.2Simple Program: Printing a Line of Text 3.3Another Simple Program: Adding Integers 3.7Using a Dialog."— Presentation transcript:

1  2002 Prentice Hall. All rights reserved. 1 Outline 3.2Simple Program: Printing a Line of Text 3.3Another Simple Program: Adding Integers 3.7Using a Dialog to Display a Message 4.4 Control Structures 4.5 If/Then Selection Structure 4.6 If/Then/Else Selection Structure 5.5 Select Case Multiple-Selection Structure 4.7 While Repetition Structure 4.8 Do While/Loop Repetition Structure 4.9 Do Until/Loop Repetition Structure 5.3 For/Next Repetition Structure 5.6 Do/Loop While Repetition Structure 5.7 Do/Loop Until Repetition Structure 5.8Using the Exit Keyword in a Repetition Structure 3.5Arithmetic Operators 3.6Equality and Relational Operators 4.10 Assignment Operators 5.9 Logical Operators 4.15 Introduction to Windows Application Programming Introduction to Visual Basic.Net Programming, Control Structures and Operators

2  2002 Prentice Hall. All rights reserved. 2 3.2 Simple Program: Printing a Line of Text Simple console application that displays a line of text When the program is run –output appears in a command window It illustrates important Visual Basic features –Comments –Modules –Sub procedures

3  2002 Prentice Hall. All rights reserved. Outline 3 1 ' Fig. 3.1: Welcome1.vb 2 ' Simple Visual Basic program. 3 4 Module modFirstWelcome 5 6 Sub Main() 7 Console.WriteLine("Welcome to Visual Basic!") 8 End Sub 9 10 End Module Welcome to Visual Basic! Single-quote character ( ' ) indicates that the remainder of the line is a comment Visual Basic console applications consist of pieces called modules The Main procedure is the entry point of the program. It is present in all console applications The Console.WriteLine statement displays text output to the console A few Good Programming Practices – Comments - Every program should begin with one or more comments – Modules - Begin each module with mod to make modules easier to identify – Procedures (subroutines) - Indent the entire body of each procedure definition one “level” of indentation

4  2002 Prentice Hall. All rights reserved. 4 3.2 Simple Program: Printing a Line of Text Now a short step-by-step explanation of how to create and run this program using the features of Visual Studio.NET IDE…

5  2002 Prentice Hall. All rights reserved. 5 3.2 Simple Program: Printing a Line of Text 1.Create the console application –Select File > New > Project… –In the left pane, select Visual Basic Projects –In the right pane, select Console Application –Name the project Welcome1 –Specify the desired location 2.Change the name of the program file –Click Module1.vb in the Solution Explorer window –In the Properties window, change the File Name property to Welcome1.vb

6  2002 Prentice Hall. All rights reserved. 6 3.2 Simple Program: Printing a Line of Text Fig. 3.2Creating a Console Application with the New Project dialog. Left paneRight pane Project name File location

7  2002 Prentice Hall. All rights reserved. 7 3.2 Simple Program: Printing a Line of Text Fig. 3.3IDE with an open console application. Editor window (containing program code)

8  2002 Prentice Hall. All rights reserved. 8 3.2 Simple Program: Printing a Line of Text Fig. 3.4Renaming the program file in the Properties window. Solution Explorer File Name property Click Module1.vb to display its properties Properties window

9  2002 Prentice Hall. All rights reserved. 9 3.2 Simple Program: Printing a Line of Text 3.Change the name of the module –Module names must be modified in the editor window –Replace the identifier Module1 with modFirstWelcome 4.Writing code –Type the code contained in line 7 of Fig. 3.1 between Sub Main() and End Sub Note that after typing the class name and the dot operator the IntelliSense is displayed. It lists a class’s members. Note that when typing the text between the parenthesis (parameter), the Parameter Info and Parameter List windows are displayed

10  2002 Prentice Hall. All rights reserved. 10 3.2 Simple Program: Printing a Line of Text 5.Run the program –To compile, select Build > Build Solution This creates a new file, named Welcome1.exe –To run, select Debug > Start Without Debugging

11  2002 Prentice Hall. All rights reserved. 11 3.2 Simple Program: Printing a Line of Text Fig. 3.5IntelliSense feature of the Visual Studio.NET IDE. Partially-typed memberMember list Description of highlighted member

12  2002 Prentice Hall. All rights reserved. 12 3.2 Simple Program: Printing a Line of Text Fig. 3.6Parameter Info and Parameter List windows. Up arrowDown arrow Parameter List window Parameter Info window

13  2002 Prentice Hall. All rights reserved. 13 3.2 Simple Program: Printing a Line of Text Fig. 3.7Executing the program shown in Fig. 3.1. Command window prompts the user to press a key after the program terminates

14  2002 Prentice Hall. All rights reserved. 14 3.2 Simple Program: Printing a Line of Text Fig. 3.8IDE indicating a syntax error. Omitted parenthesis character (syntax error) Blue underline indicates a syntax error Task List window Error description(s)

15  2002 Prentice Hall. All rights reserved. Outline 15 1 ' Fig. 3.9: Welcome2.vb 2 ' Writing line of text with multiple statements. 3 4 Module modSecondWelcome 5 6 Sub Main() 7 Console.Write("Welcome to ") 8 Console.WriteLine("Visual Basic!") 9 End Sub ' Main 11 12 End Module ' modSecondWelcome Welcome to Visual Basic! Method Write does not position the output cursor at the beginning of the next line Method WriteLine positions the output cursor at the beginning of the next line

16  2002 Prentice Hall. All rights reserved. 16 3.3 Another Simple Program: Adding Integers User input two integers –Whole numbers Program computes the sum Display result

17  2002 Prentice Hall. All rights reserved. Outline 17 Addition.vb 1 ' Fig. 3.10: Addition.vb 2 ' Addition program. 3 4 Module modAddition 5 6 Sub Main() 7 8 ' variables for storing user input 9 Dim firstNumber, secondNumber As String 10 11 ' variables used in addition calculation 12 Dim number1, number2, sumOfNumbers As Integer 13 14 ' read first number from user 15 Console.Write("Please enter the first integer: ") 16 firstNumber = Console.ReadLine() 17 18 ' read second number from user 19 Console.Write("Please enter the second integer: ") 20 secondNumber = Console.ReadLine() 21 22 ' convert input values to Integers 23 number1 = firstNumber 24 number2 = secondNumber 25 26 sumOfNumbers = number1 + number2 ' add numbers 27 28 ' display results 29 Console.WriteLine("The sum is {0}", sumOfNumbers) 30 31 End Sub ' Main 32 33 End Module ' modAddition Variable declarations begin with keyword Dim These variables store strings of characters These variables store integers values First value entered by user is assigned to variable firstNumber Method ReadLine causes program to pause and wait for user input Implicit conversion from String to Integer Sums integers and assigns result to variable sumOfNumbers Format indicates that the argument after the string will be evaluated and incorporated into the string

18  2002 Prentice Hall. All rights reserved. Outline 18 Please enter the first integer: 45 Please enter the second integer: 72 The sum is 117 Fig. 3.11Dialog displaying a run-time error. If the user types a non-integer value, such as “ hello,” a run-time error occurs

19  2002 Prentice Hall. All rights reserved. 19 3.7 Using a Dialog to Display a Message Dialogs –Windows that typically display messages to the user –Visual Basic provides class MessageBox for creating dialogs

20  2002 Prentice Hall. All rights reserved. Outline 20 SquareRoot.vb Program Output 1 ' Fig. 3.20: SquareRoot.vb 2 ' Displaying square root of 2 in dialog. 3 4 Imports System.Windows.Forms ' Namespace containing MessageBox 5 6 Module modSquareRoot 7 8 Sub Main() 9 10 ' Calculate square root of 2 11 Dim root As Double = Math.Sqrt(2) 12 13 ' Display results in dialog 14 MessageBox.Show("The square root of 2 is " & root, _ 15 "The Square Root of 2") 16 End Sub ' Main 17 18 End Module ' modThirdWelcome Sqrt method of the Math class is called to compute the square root of 2 The Double data type stores floating- point numbers Method Show of class MessageBox Line-continuation character Empty command window

21  2002 Prentice Hall. All rights reserved. 21 3.7 Using a Dialog to Display a Message Assembly –File that contain many classes provided by Visual Basic –These files have a.dll (or dynamic link library) extension. –Example Class MessageBox is located in assembly System.Windows.Forms.dll MSDN Documentation –Information about the assembly that we need can be found in the MSDN documentation –Select Help > Index … to display the Index dialog

22  2002 Prentice Hall. All rights reserved. 22 3.7 Using a Dialog to Display a Message Fig. 3.22Obtaining documentation for a class by using the Index dialog. Search string Filter Link to MessageBox documentation

23  2002 Prentice Hall. All rights reserved. 23 3.7 Using a Dialog to Display a Message Fig. 3.23Documentation for the MessageBox class. Requirements section heading MessageBox class documentation Assembly containing class MessageBox

24  2002 Prentice Hall. All rights reserved. 24 3.7 Using a Dialog to Display a Message Reference –It is necessary to add a reference to the assembly if you wish to use its classes –Example To use class MessageBox it is necessary to add a reference to System.Windows.Forms Imports –Forgetting to add an Imports statement for a referenced assembly is a syntax error

25  2002 Prentice Hall. All rights reserved. 25 3.7 Using a Dialog to Display a Message Fig. 3.24Adding a reference to an assembly in the Visual Studio.NET IDE. References folder (expanded) Solution Explorer before reference is added Solution Explorer after reference is added System.Windows.Forms reference

26  2002 Prentice Hall. All rights reserved. 26 3.7 Using a Dialog to Display a Message Fig. 3.25Internet Explorer window with GUI components. Label Button (displaying an icon) Menu (e.g., Help)Text boxMenu bar

27  2002 Prentice Hall. All rights reserved. 27 4.4 Control Structures Transfer of control –GoTo statement It causes programs to become quite unstructured and hard to follow Bohm and Jacopini –All programs could be written in terms of three control structures Sequence structure Selection structure Repetition structure

28  2002 Prentice Hall. All rights reserved. 28 4.4 Control Structures Selection Structures –If / Then Single-selection structure –If / Then / Else Double-selection structure –Select Case Multiple-selection structure

29  2002 Prentice Hall. All rights reserved. 29 4.4 Control Structures Repetition Structures –While –Do While / Loop –Do / Loop While –Do Until / Loop –Do / Loop Until –For / Next –For Each / Next

30  2002 Prentice Hall. All rights reserved. 30 Visual Basic keywords

31  2002 Prentice Hall. All rights reserved. 31 Visual Basic keywords

32  2002 Prentice Hall. All rights reserved. 32 4.5 If / Then Selection Structure A selection structure chooses among alternative courses of action. It is a single-entry/single-exit structure Example If studentGrade >= 60 Then Console.WriteLine("Passed") End If

33  2002 Prentice Hall. All rights reserved. 33 4.6 If / Then / Else Selection Structure Example If studentGrade >= 60 Then Console.WriteLine("Passed") Else Console.WriteLine("Failed") End If

34  2002 Prentice Hall. All rights reserved. 34 Nested If / Then / Else Selection Structure Test for multiple conditions by placing one structure inside the other. ElseIf keyword Example If studentGrade >= 60 Then Console.WriteLine("Good") ElseIf studentGrade >= 40 Console.WriteLine("Medium") Else Console.WriteLine("Bad") End If

35  2002 Prentice Hall. All rights reserved. 35 5.5 Select Case Multiple-Selection Structure Multiple-Selection Structure –Tests expression separately for each value expression may assume –Select Case keywords begin structure Followed by controlling expression –Compared sequentially with each case –Code in case executes if match is found –Program control proceeds to first statement after structure Case keyword –Specifies each value to test for –Followed by code to execute if test is true –Case Else Optional Executes if no match is found Must be last case in sequence

36  2002 Prentice Hall. All rights reserved. Outline 36 SelectTest.vb 1 ' Fig. 5.10: SelectTest.vb 2 ' Using the Select Case structure. 3 4 Module modEnterGrades 5 6 Sub Main() 7 Dim grade As Integer = 0 ' one grade 8 Dim aCount As Integer = 0 ' number of As 9 Dim bCount As Integer = 0 ' number of Bs 10 Dim cCount As Integer = 0 ' number of Cs 11 Dim dCount As Integer = 0 ' number of Ds 12 Dim fCount As Integer = 0 ' number of Fs 13 14 Console.Write( "Enter a grade, -1 to quit: " ) 15 grade = Console.ReadLine() 16 17 ' input and process grades 18 While grade <> -1 19 20 Select Case grade ' check which grade was input 21 22 Case 100 ' student scored 100 23 Console.WriteLine( "Perfect Score!" & vbCrLf & _ 24 "Letter grade: A" & vbCrLf ) 25 aCount += 1 26 27 Case 90 To 99 ' student scored 90-99 28 Console.WriteLine( "Letter Grade: A" & vbCrLf ) 29 aCount += 1 30 31 Case 80 To 89 ' student scored 80-89 32 Console.WriteLine( "Letter Grade: B" & vbCrLf ) 33 bCount += 1 34 Select Case begins multiple-selection structure Controlling expression First Case executes if grade is exactly 100 Next Case executes if grade is between 90 and 99, the range being specified with the To keyword Identifier vbCrLf is the combination of the carriage return and linefeed characters

37  2002 Prentice Hall. All rights reserved. Outline 37 SelectTest.vb 35 Case 70 To 79 ' student scored 70-79 36 Console.WriteLine("Letter Grade: C" & vbCrLf) 37 cCount += 1 38 39 Case 60 To 69 ' student scored 60-69 40 Console.WriteLine("Letter Grade: D" & vbCrLf) 41 dCount += 1 42 43 ' student scored 0 or 10-59 (10 points for attendance) 44 Case 0, 10 To 59 45 Console.WriteLine("Letter Grade: F" & vbCrLf) 46 fCount += 1 47 48 Case Else 49 50 ' alert user that invalid grade was entered 51 Console.WriteLine("Invalid Input. " & _ 52 "Please enter a valid grade." & vbCrLf) 53 End Select 54 55 Console.Write("Enter a grade, -1 to quit: ") 56 grade = Console.ReadLine() 57 End While 58 59 ' display count of each letter grade 60 Console.WriteLine(vbCrLf & _ 61 "Totals for each letter grade are: " & vbCrLf & _ 62 "A: " & aCount & vbCrLf & "B: " & bCount _ 63 & vbCrLf & "C: " & cCount & vbCrLf & "D: " & _ 64 dCount & vbCrLf & "F: " & fCount) 65 66 End Sub ' Main 67 68 End Module ' modEnterGrades Optional Case Else executes if no match occurs with previous Case s End Select marks end of structure

38  2002 Prentice Hall. All rights reserved. Outline 38 Program Output Enter a grade: 84 Letter Grade: B Enter a grade: 100 Perfect Score! Letter grade : A+ Enter a grade: 7 Invalid Input. Please enter a valid grade. Enter a grade: 95 Letter Grade: A Enter a grade: 78 Letter Grade: C Totals for each letter grade are: A: 2 B: 1 C: 1 D: 0 F: 0

39  2002 Prentice Hall. All rights reserved. 39 4.7 While Repetition Structure Executes an action while the condition is true Example ' writes numbers from 1 to 10 Dim number As Integer = 1 While number <= 10 Console.Write("{0} ", number) number = number + 1 End While

40  2002 Prentice Hall. All rights reserved. 40 4.8 Do While/Loop Repetition Structure Behaves like the While repetition structure Example ' writes numbers from 1 to 10 Dim number As Integer = 1 Do While number <= 10 Console.Write("{0} ", number) number = number + 1 Loop

41  2002 Prentice Hall. All rights reserved. 41 4.9 Do Until/Loop Repetition Structure Executes an action until a condition is false Example ' writes numbers from 1 to 10 Dim number As Integer = 1 Do Until number > 10 Console.Write("{0} ", number) number = number + 1 Loop

42  2002 Prentice Hall. All rights reserved. 42 5.6 Do/Loop While Repetition Structure Do / Loop While Repetition Structure –Similar to While and Do / While –Loop-continuation condition tested after body executes Loop body always executed at least once –Begins with keyword Do –Ends with keywords Loop While followed by condition Example ' writes numbers from 1 to 10 Dim number As Integer = 1 Do Console.Write("{0} ", number) number = number + 1 Loop While (number <= 10)

43  2002 Prentice Hall. All rights reserved. 43 5.7 Do/Loop Until Repetition Structure Do / Loop Until Repetition Structure –Similar to Do Until / Loop structure –Loop-continuation condition tested after body executes Loop body always executed at least once Example ' writes numbers from 1 to 10 Dim number As Integer = 1 Do Console.Write("{0} ", number) number = number + 1 Loop Until number > 10

44  2002 Prentice Hall. All rights reserved. 44 5.3 For / Next Repetition Structure For / Next counter-controlled repetition –Structure header initializes control variable, specifies final value and increment For keyword begins structure –Followed by control variable initialization To keyword specifies final value Step keyword specifies increment –Optional –Increment defaults to 1 if omitted –May be positive or negative –Next keyword marks end of structure –Executes until control variable greater (or less) than final value

45  2002 Prentice Hall. All rights reserved. 45 5.3 For / Next Repetition Structure Example ' writes numbers from 1 to 10 Dim number As Integer For counter = 1 To 10 Step 1 Console.Write("{0} ", number) Next

46  2002 Prentice Hall. All rights reserved. 46 5.4 Examples Using the For / Next Structure Examples –Vary the control variable from 1 to 100 in increments of 1 For i = 1 To 100 For i = 1 To 100 Step 1 –Vary the control variable from 100 to 1 in increments of –1 For i = 100 To 1 Step –1 –Vary the control variable from 7 to 77 in increments of 7 For i = 7 To 77 Step 7 –Vary the control variable from 20 to 2 in increments of –2 For i = 20 To 2 Step -2

47  2002 Prentice Hall. All rights reserved. Outline 47 Sum.vb Program Output 1 ' Fig. 5.5: Sum.vb 2 ' Using For/Next structure to demonstrate summation. 3 4 Imports System.Windows.Forms 5 6 Module modSum 7 8 Sub Main() 9 10 Dim sum = 0, number As Integer 11 12 ' add even numbers from 2 to 100 13 For number = 2 To 100 Step 2 14 sum += number 15 Next 16 17 MessageBox.Show("The sum is " & sum, _ 18 "Sum even integers from 2 to 100", _ 19 MessageBoxButtons.OK, MessageBoxIcon.Information) 20 End Sub ' Main 21 22 End Module ' modSum Control variable counts by 2, from 2 to 100 Value of number is added in each iteration to determine sum of even numbers Text displayed in dialog Display a MessageBox Indicate button to be OK button Indicate icon to be Information icon Text displayed in title bar

48  2002 Prentice Hall. All rights reserved. 48 5.4 Examples Using the For / Next Structure Fig. 5.6Icons for message dialogs.

49  2002 Prentice Hall. All rights reserved. 49 5.4 Examples Using the For / Next Structure Fig. 5.7Button constants for message dialogs.

50  2002 Prentice Hall. All rights reserved. Outline 50 Interest.vb 1 ' Fig. 5.8: Interest.vb 2 ' Calculating compound interest. 3 4 Imports System.Windows.Forms 5 6 Module modInterest 7 8 Sub Main() 9 10 Dim amount, principal As Decimal ' dollar amounts 11 Dim rate As Double ' interest rate 12 Dim year As Integer ' year counter 13 Dim output As String ' amount after each year 14 15 principal = 1000.00 16 rate = 0.05 17 18 output = "Year" & vbTab & "Amount on deposit" & vbCrLf 19 20 ' calculate amount after each year 21 For year = 1 To 10 22 amount = principal * (1 + rate) ^ year 23 output &= year & vbTab & _ 24 String.Format("{0:C}", amount) & vbCrLf 25 Next 26 27 ' display output 28 MessageBox.Show(output, "Compound Interest", _ 29 MessageBoxButtons.Ok, MessageBoxIcon.Information) 30 31 End Sub ' Main 32 33 End Module ' modInterest Perform calculation to determine amount in account Append year followed by the formatted calculation result and newline character to end of String output Specify C (for “currency”) as formatting code Type Decimal used for precise monetary calculations

51  2002 Prentice Hall. All rights reserved. Outline 51 Program Output

52  2002 Prentice Hall. All rights reserved. 52 5.4 Examples Using the For / Next Structure Fig. 5.9String formatting codes.

53  2002 Prentice Hall. All rights reserved. 53 5.8 Using the Exit Keyword in a Repetition Structure Exit Statements –Alter the flow of control Cause immediate exit from a repetition structure –Exit Do Executed in Do structures –Exit For Executed in For structures –Exit While Executed in While structures

54  2002 Prentice Hall. All rights reserved. Outline 54 ExitTest.vb 1 ' Fig. 5.16: ExitTest.vb 2 ' Using the Exit keyword in repetition structures. 3 4 Imports System.Windows.Forms 5 6 Module modExitTest 7 8 Sub Main() 9 Dim output As String 10 Dim counter As Integer 11 12 For counter = 1 To 10 13 14 ' skip remaining code in loop only if counter = 3 15 If counter = 3 Then 16 Exit For 17 End If 18 19 Next 20 21 output = "counter = " & counter & _ 22 " after exiting For/Next structure" & vbCrLf 23 24 Do Until counter > 10 25 26 ' skip remaining code in loop only if counter = 5 27 If counter = 5 Then 28 Exit Do 29 End If 30 31 counter += 1 32 Loop 33 Loop specified to execute 10 times Exit For statement executes when condition is met, causing loop to exit Program control proceeds to first statement after the structure counter is 3 when loop starts, specified to execute until it is greater than 10 Exit Do executes when counter is 5, causing loop to exit

55  2002 Prentice Hall. All rights reserved. Outline 55 Program Output 34 output &= "counter = " & counter & _ 35 " after exiting Do Until/Loop structure" & vbCrLf 36 37 While counter <= 10 38 39 ' skip remaining code in loop only if counter = 7 40 If counter = 7 Then 41 Exit While 42 End If 43 44 counter += 1 45 End While 46 47 output &= "counter = " & counter & _ 48 " after exiting While structure" 49 50 MessageBox.Show(output, "Exit Test", _ 51 MessageBoxButtons.OK, MessageBoxIcon.Information) 52 End Sub ' Main 53 54 End Module ' modExitTest counter is 5 when loop starts, specified to execute while less than or equal to 10 Exit While executes when counter is 7, causing loop to exit

56  2002 Prentice Hall. All rights reserved. 56 3.5 Arithmetic Operators

57  2002 Prentice Hall. All rights reserved. 57 3.6 Equality and Relational Operators

58  2002 Prentice Hall. All rights reserved. 58 4.10 Assignment Operators Binary operators –+, -, *, ^, &, / or \ variable = variable operator expression variable operator = expression Example –Addition assignment operator, += value = value + 3 value += 3

59  2002 Prentice Hall. All rights reserved. 59 4.10 Assignment Operators Fig. 4.11Assignment operators.

60  2002 Prentice Hall. All rights reserved. 60 5.9 Logical Operators Used to form complex conditions by combining simple ones –Short-circuit evaluation Execute only until truth or falsity is known –AndAlso operator Returns true if and only if both conditions are true –OrElse operator Returns true if either or both of two conditions are true

61  2002 Prentice Hall. All rights reserved. 61 5.9 Logical Operators (II) Logical Operators without short-circuit evaluation –And and Or Similar to AndAlso and OrElse respectively Always execute both of their operands Used when an operand has a side effect –Condition makes a modification to a variable –Should be avoided to reduce subtle errors –Xor Returns true if and only if one operand is true and the other false

62  2002 Prentice Hall. All rights reserved. 62 5.9 Logical Operators (III) Logical Negation –Not Used to reverse the meaning of a condition Unary operator –Requires one operand Can usually be avoided by expressing a condition differently

63  2002 Prentice Hall. All rights reserved. 63 5.9 Logical Operators

64  2002 Prentice Hall. All rights reserved. 64 5.9 Logical Operators

65  2002 Prentice Hall. All rights reserved. 65 Precedence and Associativity of Operators lower precedence higher precedence

66  2002 Prentice Hall. All rights reserved. 66 4.15 Introduction to Windows Application Programming Windows application –Consists of at least one class Inherits from class Form Form is called the superclass or base class –Keyword Class Begins a class definition and is followed by the class name –Keyword Inherits Indicates that the class inherits existing pieces from another class

67  2002 Prentice Hall. All rights reserved. 67 4.15 Introduction to Windows Application Programming Fig. 4.21IDE showing program code for Fig. 2.15. Collapsed code

68  2002 Prentice Hall. All rights reserved. 68 4.15 Introduction to Windows Application Programming Windows Form Designer generated code –Collapsed by default –The code is created by the IDE and normally is not edited by the programmer –Present in every Windows application

69  2002 Prentice Hall. All rights reserved. 69 4.15 Introduction to Windows Application Programming Fig. 4.22Windows Form Designer generated code when expanded. Expanded code

70  2002 Prentice Hall. All rights reserved. 70 4.15 Introduction to Windows Application Programming Fig. 4.23Code generated by the IDE for lblWelcome. Click here for code view Click here for design view Property initializations for lblWelcome

71  2002 Prentice Hall. All rights reserved. 71 4.15 Introduction to Windows Application Programming How IDE updates the generated code 1.Modify the file name Change the name of the file to ASimpleProgram.vb 2.Modify the label control’s Text property using the Properties window Change the property of the label to “ Deitel and Associates ” 3.Examine the changes in the code view Switch to code view and examine the code 4.Modifying a property value in code view Change the string assigned to Me.lblWelcome.Text to “ Visual Basic.NET ”

72  2002 Prentice Hall. All rights reserved. 72 4.15 Introduction to Windows Application Programming Fig. 4.24Using the Properties window to set a property value. Text property

73  2002 Prentice Hall. All rights reserved. 73 4.15 Introduction to Windows Application Programming Fig. 4.25Windows Form Designer generated code reflecting new property values. Text property

74  2002 Prentice Hall. All rights reserved. 74 4.15 Introduction to Windows Application Programming Fig. 4.26Changing a property in the code view editor. Text property

75  2002 Prentice Hall. All rights reserved. 75 4.15 Introduction to Windows Application Programming Fig. 4.27New Text property value reflected in design mode. Text property value

76  2002 Prentice Hall. All rights reserved. 76 4.15 Introduction to Windows Application Programming 5.Change the label’s Text Property at runtime Add a method named FrmASimpleProgram_Load to the class Add the statement lblWelcome.Text = "Visual Basic" in the body of the method definition 6.Examine the results of the FrmASimpleProgram_Load method Select Build > Build Solution then Debug > Start

77  2002 Prentice Hall. All rights reserved. 77 4.15 Introduction to Windows Application Programming Fig. 4.28Adding program code to FrmASimpleProgram_Load. FrmASimpleProgram_Load method

78  2002 Prentice Hall. All rights reserved. 78 4.15 Introduction to Windows Application Programming Fig. 4.29Method FrmASimpleProgram_Load containing program code.


Download ppt " 2002 Prentice Hall. All rights reserved. 1 Outline 3.2Simple Program: Printing a Line of Text 3.3Another Simple Program: Adding Integers 3.7Using a Dialog."

Similar presentations


Ads by Google