Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Chapter2

Topics :
2.1 Focus on Problem Solving: Building the Directions Application.
2.2 Focus on Problem Solving: Responding to Events
2.3 Modifying a Control’s Text Property with Code
2.4 The AutoSize, BorderStyle, and TextAlign Properties
2.5 Displaying User Messages
2.6 Clickable Images
2.7 Using Visual Studio Help
2.8 Debugging Your Application
2.1 Focus on Problem Solving:
Building the Directions Application
1. Clearly define what the application is to do.
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
2. Visualize the application running
on the computer and design its user interface.
3. Determine the controls needed.
4.Define the values of each control’s relevant properties.

5. Start Visual Studio and create the forms and other controls
Beginning the Directions application
Properties window showing Form1
Changing the Label’s font size and style
Font property in the Properties window
Adding a Label control to the Directions application
The PictureBox Control

has several properties, but the following two properties are of particular
interest:
• The Image property specifies the image file that is to be displayed by the
control.
• The SizeMode property specifies how the image is to be displayed. It can be
set to one of the following values:
• Normal
• StretchImage
• AutoSize
• CenterImage
• Zoom
Visual Studio modes: Design Mode, Run Mode, and
Break Mode
• Design Mode: this is the mode in which you create an application.
• Run mode: The application will be running on the computer, and you can
interact with it as the user.
 There are three ways to run an application from the Visual Studio
environment:
• Click the Start Debugging button ( ) on the toolbar
• Click DEBUG on the menu bar, then select Start Debugging
• Press the F5 key
• Break mode is a special mode that allows you to momentarily suspend a
running application for testing and debugging purposes. It is also the mode that
Visual Studio enters when a running application encounters a runtime error.
2.2 Focus on Problem Solving: Responding to Events
such as mouse clicks and keyboard input.
Adding a Label control for the written directions
Adding button and coding:
2.3 Modifying a Control’s Text Property with Code
lblDirections.Visible = True
lblMessage.Text = "Programming is fun!
code
 Public Class Form1
Private Sub btnExit_Click(...) Handles btnExit.Click
' End the application.
Me.Close()
End Sub
Private Sub btnFeet_Click(...) Handles btnFeet.Click
' Display the conversion to feet.
lblMessage.Text = "1 Kilometer = 3,281 feet"
End Sub
Private Sub btnInches_Click(...) Handles btnInches.Click
' Display the conversion to inches.
lblMessage.Text = "1 Kilometer = 39,370 inches"
End Sub
Private Sub btnMiles_Click(...) Handles btnMiles.Click
' Display the conversion to miles.
lblMessage.Text = "1 Kilometer = 0.6214 miles"
End Sub
Private Sub btnYards_Click(...) Handles btnYards.Click
' Display the conversion to yards.
lblMessage.Text = "1 Kilometer = 1,093.6 yards"
End Sub
End Class
2.4 The AutoSize, BorderStyle, and TextAlign Properties
- The AutoSize property is a Boolean property that is set to True by default.
- The Label control’s BorderStyle property may have one of three values:
None, FixedSingle, and Fixed3D. The property is set to None by default.
- By default, a label’s text is aligned with the top and left edges of the label’s
bounding box.
Changing a Label’s TextAlign Property with Code

By assigning one of the following values to the


property:
ContentAlignment.TopLeft
ContentAlignment.TopCenter
ContentAlignment.TopRight
ContentAlignment.MiddleLeft
ContentAlignment.MiddleCenter
ContentAlignment.MiddleRight
ContentAlignment.BottomLeft
ContentAlignment.BottomCenter
ContentAlignment.BottomRight
For example:
lblReportTitle.TextAlign = ContentAlignment.MiddleCenter
2.5 Displaying User Messages
 A message box is a small window, sometimes referred to as a dialog box, that displays a
message.
 MessageBox.Show("Hello World!").
 The StatusStrip control uses a Label to display program status information and messages to
the user.
 Private Sub btnOne_Click() Handles btnOne.Click
' Display the French word for one.
lblStatus.Text = "un"
End Sub
Private Sub btnTwo_Click() Handles btnTwo.Click
' Display the French word for two.
lblStatus.Text = "deux"
End Sub
Public Class Form1
Private Sub btnOne_Click(...) Handles btnOne.Click
' Display the French word for one.
MessageBox.Show("un")
End Sub
Private Sub btnTwo_Click(...) Handles btnTwo.Click
' Display the French word for two.
MessageBox.Show("deux")
End Sub
Private Sub btnThree_Click(...) Handles btnThree.Click
' Display the French word for three.
MessageBox.Show("trois")
End Sub
Private Sub btnFour_Click(...) Handles btnFour.Click
' Display the French word for four.
MessageBox.Show("quatre")
End Sub
Private Sub btnFive_Click(...) Handles btnFive.Click
MessageBox.Show("cinq")
End Sub
Private Sub btnExit_Click(...) Handles btnExit.Click
Me.Close()
End Sub
End Class
code
Private Sub btnOne_Click() Handles btnOne.Click
' Display the French word for one.
lblStatus.Text = "un"
End Sub
Private Sub btnTwo_Click() Handles btnTwo.Click
' Display the French word for two.
lblStatus.Text = "deux"
End Sub
2.7 Using Visual Studio Help
 The documentation for Visual Studio and Visual Basic is contained in the
MSDN (Micro-soft Developer Network) Library. The MSDN Library
contains a wealth of information,
including code samples, tutorials, and technical articles.
 As shown in the next figure , you can access the MSDN Library by clicking

HELP on the Visual Studio menu bar, and then selecting View Help. (Or,
you can simply press Ctrl+F1.)
This opens the MSDN Library in your Web browser.
2.8 Debugging Your Application
 At some point, most applications contain bugs (errors) that prevent the
application from operating properly. In this section, you learn
fundamental debugging techniques.
Visual Basic reports errors in your project as soon as it finds them.
 In general, there are two types of errors: compile errors and runtime

errors.

You might also like