Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 3131.01 Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha University of Houston – Clear Lake.

Similar presentations


Presentation on theme: "CSCI 3131.01 Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha University of Houston – Clear Lake."— Presentation transcript:

1 CSCI 3131.01 Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha University of Houston – Clear Lake

2 Acknowledgement Dr. Xinhua Chen And Starting Out with Visual Basic 2010 by Tony Gaddis and Kip Irvine

3 Topics Building the Directions application Responding to events Modifying properties of a control with code: Text, Autosize, BorderStyle, and TextAlign properties Clickable images Using Visual Studio Help Debugging an application

4 Steps to Create an Application 1.Clearly define what the application is to do. 2.Visualize the application running on the computer and design its user interface 3.Make a list of the controls needed. 4.Define the values of each control’s relevant properties. 5.Start Visual Basic and create the forms and other controls

5 Clearly Define the Task of the Application Purpose: Display a map to the Highlander Hotel Input: None Process: Display a form Output: Display on the form a graphic image showing a map Specifications of the Application

6 Visualize and Design the User Interface

7 Points to Watch while Designing User Interface Follow the convention of Windows Graphic User Interface (GUI) design. Most Windows application consists of (1) a main (primary) window, (2) possibly some other primary windows, and (3) one or more secondary windows, called dialog boxes. The Windows Notepad application uses the primary window for editing the document; it uses a dialog box for selecting the font related properties. Primary windows can be resized, minimized, maximized and closed by the user.

8 Using Graphics, Fonts and Color in User Interface Using graphics, fonts and colors appropriately helps the information flow more efficiently from the user interface to the users. Inappropriate use of graphics, fonts and colors distracts the users and may also offend some of the users.

9 List the Controls Needed Control TypeControl NameDescription FormForm1A small form that will serve as (Default Name)the window onto which the other controls will be placed LabelLabel1Displays the message (Default Name)"Directions to the Highlander Hotel" PictureBoxPictureBox1Displays the graphic image (Default Name)showing the map to the hotel

10 Define Control Relevant Property Values Form –Name: Form1 –Text: "Directions" Label –Name: Label1 –Text: "Directions to the Highlander Hotel" –TextAlign: MiddleCenter –Font: Microsoft sans serif, bold, 18 point PictureBox –Name: PictureBox1 –Picture: HotelMap.jpg –SizeMode: StretchImage

11 Use VB to Create the Application Establish the Form and set its Text property Add a Label control –Position and resize it on the form –Set Text, TextAlign, and Font properties Add a PictureBox control –Position and resize it on the form –Set Image property to display HotelMap.jpg Run the application Close and save the application

12 Responding to Events An Application Responds to Events, Such As Mouse Clicks Keyboard Input The code that handles the events are called Event Handlers or Event Procedures Write the Event Procedures for the Directions Application

13 Augment the Hotel Application Now the hotel owner wants to add an option to view written directions:

14 Controls to be Added Control TypeControl NameDescription LabellblDirectionsDisplays written directions to the hotel ButtonbtnDisplayDirectionsWhen clicked, causes lblDisplayDirections text to appear on the form ButtonbtnExitStops the application when clicked

15 Control Properties Label: –Name: lblDirections –Text: “Traveling on I-89, take…” –Visible: False Button: –Name: btnDisplayDirections –Text: “Display Directions” Button: –Name: btnExit –Text: "Exit"

16 Method btnDisplayDirections_Click Private Sub btnDisplayDirections_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnDisplayDirections.Click ' Make the directions visible lblDirections.Visible = True End Sub Line Continuation Mark Name of the event the procedure responds to Name of the control that owns the event procedure Marks the beginning of this event procedure Makes the control lblDirections visible: Assigns the value True to the Visible Property of the lblDirections control. Event handled by this procedure

17 Syntax for Referring to the Value of a Control's Property Specify the control name (lblDirections) Then a dot Then the PropertyName (Visible) For example: –lblDirections.Visible –refers to the Visible property of the lblDirections control –The visible property values may only be true or false

18 Syntax for an Assignment Statement Specify the item to receive the value Then the equal symbol Then the value to be assigned For example: –lblDirections.Visible = True –Assigns the value True to the Visible property of the lblDirections control –Causes the text of the lblDirections control to become visible to the user

19 Method btnExit_Click Private Sub btnExit_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnExit.Click ' End the application by closing the window Me.Close() End Sub Line Continuation Mark Name of the event the procedure responds to Name of the control that owns the event procedure Marks the beginning of this event procedure Closes the current form, referred to as Me, and ends the program Event handled by this procedure

20 Modifying the Text Property in Code Private Sub btnFeet_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnFeet.Click ' Display the conversion to feet. lblMessage.Text = "1 Kilometer = 3,281 feet" End Sub Assigns the string to the right of the equal sign to the text property of lblMessage This replaces the previous text property of lblMessage with the new value shown

21 AutoSize Property for Labels AutoSize 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

22 BorderStyle Property for Labels BorderStyle determines the look of the box –None (the default) means no border –FixedSingle results in a border one pixel wide –Fixed3D gives the border a recessed 3-dimensional look

23 TextAlign Property for Labels The value of TextAlign establishes the alignment (or justification) or the text: TopLeft TopCenter TopRight The assignment statement below forces the text of lblTitle to appear in the middle center of the label lblTitle.TextAlign = ContentAlignment.MiddleCenter MiddleLeft MiddleCenter MiddleRight BottomLeft BottomCenter BottomRight

24 Clickable Images Controls other than Buttons can have Click event procedures. PictureBox controls can respond to mouse clicks For example, if a national flag image is clicked, you may use a label to display the name of the country: Private Sub picUSA_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles picUSA.Click ' Display the country name lblMessage.Text = "United States of America" End Sub

25 Microsoft Document Explorer The Visual Studio Help, also called Microsoft Document Explorer, contains these options: –How Do I – a task-based topics list by category –Search – find help topics using words/phrases –Contents – displays a table of contents for help –Index – Search using predefined keywords –Favorites help – lets you bookmark help topics –Dynamic help – help for current task performed

26 Context-Sensitive Help (F1 Key) Displays information about whatever feature the user is currently focused on For example: –Click on a Button control –Press F1 –Help explains all about the Button control –Click on a Label control –Press F1 –Help explains all about the Label control

27 Debugging: Compilation Errors 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

28 Debugging: Runtime Errors 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


Download ppt "CSCI 3131.01 Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha University of Houston – Clear Lake."

Similar presentations


Ads by Google