Not Able to Set Cursor Focus in Sub Window

phoebe_ccy

Member
Joined
Oct 12, 2009
Messages
6
Programming Experience
Beginner
Hi, currently I have a Customer Master screen. Inside the screen, I have a button which call the Customer sub window.

When I call the sub window, I want the set the cursor at Textbox1.

The problem is, when I call the window for 1st time, the cursor stops at Textbox1. When I call the window for 2nd or 3rd time, the cursor stops at other textbox or buttons inside the sub window.

Seems like Textbox1.focus is not working.


'***************Calling sub window from main screen**********

Private Sub BTN_CUSTOMER_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_SEARCH.Click

With Me
w_Form = WSW001
w_Form.WSW001_Load()
w_Form.ShowDialog()

End With
End Sub



'**************Screen Load for Sub Window***************

Public Sub WSW001_Load()

Me.Textbox1.Text = ""
Me.Textbox1.Focus()

End Sub
 
First up, you shouldn't be calling Focus. You should be calling Select.
MSDN said:
Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
As for the issue, it's no use doing things like that in the Load event handler. When the form is displayed for the first time, which occurs AFTER the Load event, the form will be set to its initial state. That includes setting focus to the first control in the Tab order. That overrides what you did. If you want a specific control to have focus when the form is displayed then your first choice should be to make that control first in the Tab order.

If that's not possible for whatever reason, the alternative is to select your control in the Shown event handler, which is executed AFTER the form is displayed for the first time.
 
Back
Top