JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Resetting the Focus after a Button Click

When you click a button, the input focus is transferred to the button—and in some cases, you don't want that to happen. For example, say you've got a word processor program based on a rich text control, and that you have a button labeled "Search" in the program. When the user clicks the button, they can search for target text in the rich text box using that box's Find method, but the focus remains on the button the user clicked. When the user starts typing again, nothing appears in the rich text control, because the focus is still on the button. So how do you transfer the focus back to the rich text box?

You do that with the control's Focus method, and that's something you frequently do in real programs after button clicks. This is how it might look in code:

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    RichTextBox1.Find("Visual Basic")
End Sub

Now, when the user clicks the button and starts typing again, the focus will be back on the rich text box, as it should be.


Buttons also have two events—GotFocus and LostFocus—that can tell you when your button has gotten or lost the focus.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor