梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
To make the application do something�察�you need to think in terms of events。 For example�察 �
if you have a garage with an automatic door opener�察�you would expect that pressing the remote
control button would open the garage door when it¨s closed and close the door when it¨s open。
The automatic garage door manufacturer associated the event of pushing the remote control
button with the action of either opening or closing the garage door。 In WindowsApplication�察 �
we¨ll associate the clicking of the button with the action of showing text in the text box。
Select the button on the form in Visual Basic Express and double´click it。 The work area
changes to source code�察�with the cursor in the Button1_Click�┌� function。 Add this source code
to the function�此�
TextBox1。text = ;hello�察�world;
Figure 1´7 illustrates the procedure for associating an event with an action。
Figure 1´7。 Associating the button click event with the action of adding text to the text box
Note that TextBox1 is the name of the text box you added to the form。 This name is gener
ated by Visual Basic Express�察�just as it generated a default name for the button。 You can change
the default names ��through each control¨s Properties window���察�but we¨ve left the default for
this example。
Adding an action to an event is very simple when following the instructions shown in
Figure 1´7。 The simplicity is due to Visual Basic Express�察�and not because the event or action is
´´´´´´´´´´´´´´´´´´´´´´Page 33´´´´´´´´´´´´´´´´´´´´´´´
C H AP TE R 1 * R E AD Y �察 �ST E AD Y �察 �G O� �11
simple。 Visual Basic Express makes the assumption that when you double´click a control�察�you
want to modify the default event of the control�察�and as such�察�automatically generates the code
in step 3 of Figure 1´7。 In the case of a button�察�the default event is the click event�察�that is�察�the
event that corresponds to a user clicking the button。 The assumption of the click event being
the default event for a button is logical。 Other controls have different default events。 For example�察 �
double´clicking a TextBox control will generate the code for the text´changed event。
Run the application by pressing Ctrl��F5�察�and then click the button。 The text box fills with
the text ^hello�察�world。 ̄ Congratulations�察�you¨ve just finished your first Visual Basic application。
You have associated an event with an action�此�the button click with the text display。 Associ
ating events with actions is the basis of all Windows applications。
Adding ments to the Application
Now that you have a working program�察�it would be good to document what it does�察�right there
in the source code。 Then if you e back to the application in the future�察�you won¨t be puzzled
by your previous work。 In fact�察�you may not even be the person who maintains your code�察�so
leaving ments in the code to help explain it is definitely good practice。 Even if you know
you will be maintaining the code forever�察�treat your future self as a stranger。 You may be surprised
how long it takes to decipher code you have written when revisited months or years later。
To add a single´line ment�察�use the following syntax�此�
' A single´line ment
Anything after the ' on the same line is ignored by the piler and is not included in the
final application。 Let¨s document our Windows application�此�
' When the user clicks the button�察�we display text in the text box
Private Sub Button1_Click��ByVal sender As System。Object�察 �
ByVal e As System。EventArgs�� Handles Button1。Click
TextBox1。Text = ;hello�察�world;
End Sub
The Visual Basic language is a single´line language。 This means that a statement must be
part of a single line。 Let¨s look at a single statement�此�
TextBox1。Text = ;hello�察�world;
This line of code is a single statement because it is considered an assignment of one variable
by another piece of source code。 You could not write the statement as follows�此�
TextBox1。Text =
;hello�察�world;
When the statement is broken into two lines of source code�察�the Visual Basic piler sees
it as two statements。 Since those two statements are not plete�察�a pilation error will
result。 If you need to break a single statement over two lines�察�you must let the piler know
by adding the line´continuation character!an underscore ��_��!at the end of the continued
code�察�as follows�此�
TextBox1。Text = _
;hello�察�world;
´´´´´´´´´´´´´´´´´´´´´´Page 34´´´´´´´´´´´´´´´´´´´´´´´
12 CH AP T E R 1 * R E A DY �察 �ST E A DY �察 �G O � �
Navigating the User Controls of the Solution
When you are writing your code�察�your most important form of navigation is the Solution Explorer。
The Solution Explorer is the tree control that contains the references to your solutions and
projects。 Consider the Solution Explorer as your developer dashboard�察�which you can use to
fine´tune how your application is assembled and executed。
I suggest that you take a moment to click around the Solution Explorer。 Try some right
clicks on various elements。 The context´sensitive click is a fast way of fine´tuning particular
aspects of your solution and project。 However�察�when clicking�察�please do not click OK in any
dialog box�察�for now�察�click Cancel so that any changes you may have made are not saved。
To the right of the Solution Explorer is your work area。 The work area is where you write
your code or edit your user interface。 The work area will display only a single piece of informa
tion�察�which could be some code�察�a user interface�察�or a project。 As you saw earlier�察�when you
double´click Form1。vb in the Solution Explorer�察�the work area displays the form related to the
Form1。vb file。
Now that you have an idea of how the IDE works�察�let¨s continue with our examples。 Next
up is the console application。
Creating the Console Application
A console application is a text´based application。 This means that rather than displaying a GUI�察 �
it uses a mand´line interface。
The console has a very long history because the console was the first way to interact with a
puter。 Consoles are not very user´friendly and bee very tedious for any plex oper
ations�察�yet some people claim that a console is all you need。 ��See http��//en。wikipedia。org/
wiki/mand_line_interface for more information about the console。��
Writing to the console works only if the currently running application has a console。 To
open the console in Windows�察�select Start Run and type cmd in the dialog box。 When you test
a console application�察�Visual Basic Express opens a console for you。
Visual Basic Express can create�察�build�察�and manage console applications。
Adding a Console Application Project to the Solution
We will now create an application that outputs the text ^hello�察�world ̄ to the console。 Follow
these steps to add the new project to the ThreeExamples solution�此�
1。 Choose File Add New Project。
2。 Make sure the location is the same as that of WindowsApplication。
3。 Select Console Application and change the name to ConsoleApplication。
The Solution Explorer changes to show the additional project and now