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 file 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

MessageBox.Show("Hello World shown " + Count.ToString() + " times.", "Hello World Message Box", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

Code snippet from Forml

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 notifies you of the location without interrupting your work.

Was this article helpful?

0 0

Post a comment