Datasheet
42
❘
CHAPTER 1 VISUAL STUDIO 2010
Visual Studio adds code to the Form1.Designer.vb fi le. These changes occur in addition to the default
method implementation shown in the editable portion of your source code.
Adding XML Comments
One of Visual Studio ’ s features is the capability to generate an XML comments template for Visual Basic.
XML comments are a much more powerful feature than you might realize, because they are also recognized
by Visual Studio for use in IntelliSense. To add a new XML comment to your handler, go to the line before
the handler and type three single quotation marks: ‘ ‘ ‘ . This triggers Visual Studio to replace your single
quotation marks with the following block of comments. You can trigger these comments in front of any
method, class, or property in your code:
''' < summary >
'''
''' < /summary >
''' < param name="sender" > < /param >
''' < param name="e" > < /param >
''' < remarks > < /remarks >
Visual Studio provides a template that offers a place to include a summary of what this method does.
It also provides placeholders to describe each parameter that is part of this method. Not only are the
comments entered in these sections available within the source code, when it ’ s compiled you ’ ll also fi nd an
XML fi le in the project directory, which summarizes all your XML comments and can be used to generate
documentation and help fi les for the said source code. By the way, if you refactor a method and add new
parameters, the XML comments also support IntelliSense for the XML tags that represent your parameters.
Customizing the Event Handler
Now customize the code for the button handler, as this method doesn ’ t actually do anything by default.
Start by adding a new line of code to increment the property Count you added to the form earlier. Next,
use the System.Windows.Forms.MessageBox class to open a message box and show the message indicating
the number of times the Hello World button has been pressed. Fortunately, because that namespace is
automatically imported into every source fi le in your project, thanks to your project references, you can
reference the MessageBox.Show method directly. The Show method has several different parameters; and
as shown in Figure 1 - 24, not only does the IDE provide a ToolTip for the list of parameters, it also provides
help regarding the appropriate value for individual parameters.
The completed call to
MessageBox.Show should look similar to the following code block. Note that the
underscore character is used to continue the command across multiple lines. In addition, unlike previous
versions of Visual Basic, for which parentheses were sometimes unnecessary, in .NET the syntax best
practice is to use parentheses for every method call:
Private Sub ButtonTest_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ButtonTest.Click
Count += 1
MessageBox.Show("Hello World shown " + Count.ToString() + " times.",
"Hello World Message Box",
MessageBoxButtons.OK,
MessageBoxIcon.Information)
End Sub
Code snippet from Form1
Once you have entered this line of code, you may notice a squiggly line underneath some portions of your
text. This occurs when there is an error in the line you have typed. The Visual Studio IDE works more like
the latest version of Word. It highlights compiler issues while allowing you to continue working on your
code. Visual Basic is constantly reviewing your code to ensure that it will compile; and when it encounters a
problem it immediately notifi es you of the location without interrupting your work.
CH001.indd 42CH001.indd 42 4/5/10 11:56:55 AM4/5/10 11:56:55 AM