STARTING OUT WITH Visual Basic 2008 FOURTH EDITION
STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis Haywood Community College Kip Irvine Florida International University Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 1
Chapter 2 Creating Applications With Visual Basic Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Introduction n Develop your first application: n Display a map and written directions to the Highlander Hotel n Use a form with Labels n Use a Picture. Box control n Use Buttons n Write an event procedure Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 3
2. 1 Building the Directions Application In This Section You Create Your First Visual Basic Application: a Window That Displays a Map and Road Directions to a Hotel Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Define What the Program is To Do n n Purpose: Display a map to the Highlander Hotel Input: None Process: Display a form Output: Display a graphic image showing a map on the form Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 5
Visualize and Design the User Interface n Below is a sketch of the form to be shown Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 6
List the Controls Needed Control Type Control Name Description Form 1 A small form that will serve as (Default Name) the window onto which the other controls will be placed Label 1 Displays the message (Default Name) "Directions to the Highlander Hotel" Picture. Box 1 Displays the graphic image (Default Name) showing the map to the hotel Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 7
Define Control Relevant Property Values n n n Form n Name: Form 1 n Text: "Directions" Label n Name: Label 1 n Text: "Directions to the Highlander Hotel" n Text. Align: Middle. Center n Font: Microsoft sans serif, bold, 18 point Picture. Box n Name: Picture. Box 1 n Picture: Hotel. Map. jpg n Size. Mode: Stretch. Image Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 8
Use VB to Create the Application n n Establish the Form and set its Text property Add a Label control n Position and resize it on the form n Set Text, Text. Align, and Font properties Add a Picture. Box control n Position and resize it on the form n Set Image property to display Hotel. Map. jpg Run the application Close and save the application Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 9
Project Organization on Disk n n User creates a new project in Visual Studio n A solution and a folder are created at the same time with the same name as the project n The project belongs to the solution n Multiple projects can be included in a solution The folder stores files related to the project including: n A solution file (. sln) n A project file (. vbproj) Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 10
Opening an Existing Project n n n Use Recent Projects list on Start Page n Provided it hasn’t been moved or deleted Use Open Project button on Start Page n Then browse using Open Project dialog box Use Open Project option on File menu n Then browse using Open Project dialog box Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 11
Properties Window n n Used to view and modify the property values of a given object Two views of the properties are available: n Alphabetic (across all properties) n Categorized (groups properties by logical use) Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 12
2. 2 Responding to Events An Application Responds to Events, Such As Mouse Clicks and Keyboard Input, by Executing Code Known As Event Procedures Write the Event Procedures for the Directions Application Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Augment the Hotel Application n Now the hotel owner wants to add an option to view written directions: Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 14
Controls to be Added Control Type Control Name Description Label lbl. Directions Displays written directions to the hotel Button btn. Display. Directions When clicked, causes lbl. Display. Directions text to appear on the form Button btn. Exit Stops the application when clicked Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 15
Control Properties n n n Label: n Name: lbl. Directions n Text: "Traveling on I-89, take…“ n Visible: False Button: n Name: btn. Display. Directions n Text: "Display Directions“ Button: n Name: btn. Exit n Text: "Exit" Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 16
Method btn. Display. Directions_Click Marks the beginning of this event procedure Name of the control that owns the event procedure Name of the event the procedure responds to Line Continuation Mark Private Sub btn. Display. Directions_Click(By. Val sender As System. Object, _ By. Val e As System. Event. Args) Handles btn. Display. Directions. Click ' Make the directions visible lbl. Directions. Visible = True End Sub Event handled by this procedure Makes the control lbl. Directions visible: Assigns the value True to the Visible Property of the lbl. Directions control. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 17
Syntax for Referring to the Value of a Control's Property n n Specify the control name (lbl. Directions) Then a dot Then the Property. Name (Visible) For example: n lbl. Directions. Visible n n refers to the Visible property of the lbl. Directions control The visible property values may only be true or false Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 18
Syntax for an Assignment Statement n n Specify the item to receive the value Then the equal symbol Then the value to be assigned For example: n lbl. Directions. Visible = True n n Assigns the value True to the Visible property of the lbl. Directions control Causes the text of the lbl. Directions control to become visible to the user Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 19
Method btn. Exit_Click Marks the beginning of this event procedure Name of the control that owns the event procedure Name of the event the procedure responds to Line Continuation Mark Private Sub btn. Exit_Click(By. Val sender As System. Object, _ By. Val e As System. Event. Args) Handles btn. Exit. Click ' End the application by closing the window Me. Close() End Sub Event handled by this procedure Closes the current form, referred to as Me, and ends the program Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 20
Use Visual Basic to Update the Application n Place the label and the buttons on the form Enter the code for the two procedures Test the application Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 21
Additional Properties n n Color properties for a control: n Back. Color: Sets the background (fill) color n Fore. Color: Sets the foreground (text) color Border style properties for a form: n Sizable: (Default) Has min, max, and close buttons; can be resized by dragging edges n Fixed 3 D: Has a 3 D look; min, max, and close buttons; cannot be resized n Fixed. Single: Has single line border; min, max, and close buttons; cannot be resized Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 22
2. 3 Modifying the Text Property With Code Quite Often, You Will Need to Change a Control’s Text Property With Code This Is Done With an Assignment Statement Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Modifying the Text Property in Code n n Suppose a form is established with a label lbl. Message whose Text property is: 1 Kilometer = ? And on a btn. Feet button click, we want to change the value of the text property to: 1 Kilometer = 3, 281 feet Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 24
Modifying the Text Property in Code Private Sub btn. Feet_Click(By. Val sender As System. Object, _ By. Val e As System. Event. Args) Handles btn. Feet. Click ' Display the conversion to feet. lbl. Message. Text = "1 Kilometer = 3, 281 feet" End Sub Assigns the string to the right of the equal sign to the text property of lbl. Message This replaces the previous text property of lbl. Message with the new value shown Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 25
2. 4 The Auto. Size, Border. Style, and Text. Align Properties The Label Control’s Auto. Size Property Allows a Label to Change Size Automatically to Accommodate the Amount of Text in its Text Property The Border. Style Property Allows You to Set a Border Around a Label Control Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Auto. Size Property for Labels n n n Auto. Size is a Boolean (either True or False) Property of labels False (the default) means the box size will not change, regardless of the amount of text assigned to it True means the box will automatically resize itself to fit the amount of text assigned to it Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 27
Border. Style Property for Labels n Border. Style determines the look of the box n None (the default) means no border n Fixed. Single results in a border one pixel wide n Fixed 3 D gives the border a recessed 3 dimensional look Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 28
Text. Align Property for Labels n n The value of Text. Align establishes the alignment (or justification) or the text: n Middle. Left n Bottom. Left n Top. Left n Middle. Center n Bottom. Center n Top. Center n Middle. Right n Bottom. Right n Top. Right The assignment statement below forces the text of lbl. Title to appear in the middle center of the label lbl. Title. Text. Align = Content. Alignment. Middle. Center Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 29
2. 5 Clickable Images Controls Other Than Buttons Have Click Event Procedures Picture. Box Controls Can Respond to Mouse Clicks Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Picture. Box Control n n n As we saw earlier the Image Property can be set to a graphic image of some sort The flag images in Tutorial 2 -16 are clickable The click event can be handled by code to take whatever action is desired Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 31
Picture. Box Click Event code n When Picture. Box pic. USA is clicked, the lbl. Message text property is set to display United States of America Private Sub pic. USA_Click(By. Val sender As System. Object, _ By. Val e As System. Event. Args) Handles pic. USA. Click ' Display the country name lbl. Message. Text = "United States of America" End Sub Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 32
2. 6 Using Visual Studio Help Learn to Use the Visual Studio Help System Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Microsoft Document Explorer n The Visual Studio Help, also called Microsoft Document Explorer, contains these options: n How Do I – a task-based topics list by category n Search – find help topics using words/phrases n Contents – displays a table of contents for help n Index – Search using predefined keywords n Favorites help – lets you bookmark help topics n Dynamic help – help for current task performed Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 34
Context-Sensitive Help (F 1 Key) n n Displays information about whatever feature the user is currently focused on For example: n Click on a Button control n Press F 1 n Help explains all about the Button control n Click on a Label control n Press F 1 n Help explains all about the Label control Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 35
2. 7 Debugging Your Application At Some Point, Most Applications Contain Bugs, or Errors That Prevent the Application From Operating Properly Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Types of Errors: Compile Errors n n n These are errors in the syntax (form) of your program Visual Basic will inform you of these errors as soon as the code is entered The area of the error will be underlined with a jagged blue line A description of the error will be given in the Error List window Display this window by selecting Error List from the View menu option Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 37
Types of Errors: Runtime Errors n n n Some errors occur as your program runs These are different from syntax errors which occur as the code is entered by the programmer Runtime errors occur when Visual Basic attempts to perform an operation that cannot be executed Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2 - 38
- Slides: 38