JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Handling Button Clicks

We've covered this topic before, as far back as Chapter 1, but for completeness, I'll include it again here; you respond to button clicks with the button'sClick event. To add a click event handler, just double-click the button at design time, adding a Sub procedure such as this one to your code:

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click


End Sub

Place the code you want to execute when the button is clicked in this Sub procedure; this code is from the Buttons example on the CD-ROM:

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    TextBox1.Text = "Hello from Visual Basic"
End Sub

Here, the sender argument is the button object itself that caused the event, and the e argument is a simple EventArgs object that doesn't contain any additional useful information, such as where the button was clicked. (For that kind of information, see the MouseDown event in "Handling Mouse Events" in Chapter 4.)

All types of buttons have a Click event-they wouldn't be much use otherwise-as well as a double-click event, DoubleClick (formerly DblClick in VB6 and before). Note that if you double-click a checkbox, you select and then deselect it (or deselect and then select it), so you're back to where you started. If you double-click a radio button, however, you select it, no matter what its original state, and cause a DoubleClick event.

Related solution:

Found on page:

Handling Mouse Events

199

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor