320 likes | 524 Views
CSCI 3131.01 Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha University of Houston – Clear Lake. Acknowledgement Dr. Xinhua Chen And Starting Out with Visual Basic 2010 by Tony Gaddis and Kip Irvine. Topics Building the Directions application
E N D
CSCI 3131.01 Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha University of Houston – Clear Lake
Acknowledgement Dr. Xinhua Chen And Starting Out with Visual Basic 2010 by Tony Gaddis and Kip Irvine
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
Steps to Create an Application • Clearly define what the application is to do. • Visualize the application running on the computer and design its user interface • Make a list of the controls needed. • Define the values of each control’s relevant properties. • Start Visual Basic and create the forms and other controls
Specifications of the Application 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
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.
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.
List the Controls Needed Control TypeControl NameDescription Form Form1 A small form that will serve as (Default Name) the window onto which the other controls will be placedLabel Label1 Displays the message (Default Name) "Directions to the Highlander Hotel"PictureBox PictureBox1 Displays the graphic image (Default Name) showing the map to the hotel
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
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
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
Augment the Hotel Application • Now the hotelowner wants toadd an optionto view writtendirections:
Controls to be Added Control TypeControl NameDescriptionLabel lblDirections Displays written directions to the hotelButton btnDisplayDirections When clicked, causes lblDisplayDirections text to appear on the formButton btnExit Stops the application when clicked
Control Properties • Label: • Name: lblDirections • Text: “Traveling on I-89, take…” • Visible: False • Button: • Name: btnDisplayDirections • Text: “Display Directions” • Button: • Name: btnExit • Text: "Exit"
Method btnDisplayDirections_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 SubbtnDisplayDirections_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnDisplayDirections.Click ' Make the directions visible lblDirections.Visible = True End Sub Event handled by this procedure Makes the control lblDirections visible: Assigns the value True to the Visible Property of the lblDirections control.
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
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
Method btnExit_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 SubbtnExit_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 Event handled by this procedure Closes the current form, referred to as Me, and ends the program
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 propertyof lblMessage with the new value shown
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
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
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 labellblTitle.TextAlign = ContentAlignment.MiddleCenter • BottomLeft • BottomCenter • BottomRight • MiddleLeft • MiddleCenter • MiddleRight
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
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
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
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
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