Setting focus

  • Thread starter Thread starter Howard Kaikow
  • Start date Start date
H

Howard Kaikow

I have the following as the only statement in a Form's Load event:

txtDriveLetter.Focus()

But the textbox is not receiving the focus.

How to fix?
 
Hello Howard,


Control.Focus() is only working if the handle for that control is already
created. If you call it in Form_OnLoad, it will be too early since the handles
for child controls haven't been created yet. Try to call it after that, i.e.
Activated event with some flag to make sure it will be only called once when
the form first gets activated
 
Howard Kaikow said:
I have the following as the only statement in a Form's Load event:

txtDriveLetter.Focus()

But the textbox is not receiving the focus.

This doesn't work because the focus can only be set to controls which are
visible and enabled. You may want to call the control's 'Select' method
instead which will cause the focus to be set to the control when it becomes
visible.
 
Nat said:
Hello Howard,


Control.Focus() is only working if the handle for that control is already
created. If you call it in Form_OnLoad, it will be too early since the handles
for child controls haven't been created yet. Try to call it after that, i.e.
Activated event with some flag to make sure it will be only called once when
the form first gets activated

Activated event does the job.

Butut, the Activated event gets called every time if I am running from the
source and step thru the code.
So if I have a breakpoint in the activated event, it's a problem.
 
Herfried K. Wagner said:
This doesn't work because the focus can only be set to controls which are
visible and enabled. You may want to call the control's 'Select' method
instead which will cause the focus to be set to the control when it becomes
visible.

Ah, that does seem to work better in the Load event.
 
Herfried K. Wagner said:
This doesn't work because the focus can only be set to controls which are
visible and enabled. You may want to call the control's 'Select' method
instead which will cause the focus to be set to the control when it becomes
visible.

Your suggestion works, but where is it documented that the action is delayed
until the control becomes visible?
 
As an alternative, I have been using the following technique to set the
focus to a specific field using javascript and the
RegisterStartupScript method. I first added the following procedure to
a class I called Utilities.

Public Class Utilities
Public Shared Sub SetFocus(ByVal ctrl As Control, ByVal myPage As
Page)
' Define the JavaScript function for the specified control.
Dim focusScript As String = "<script language='javascript'>" &
_
"document.getElementById('" + ctrl.ClientID & _
"').focus();</script>"

' Add the JavaScript code to the page.
If (Not myPage.IsStartupScriptRegistered("FocusScript")) Then
myPage.RegisterStartupScript("FocusScript", focusScript)
End If
End Sub
End Class

To call the setFocus script from any page just use the following line
in the pageload event where 'Me.txtLastName' is the Id of the field you
want to set the focus to.

'Set focus to desired field
Utilities.SetFocus(Me.txtLastName, Me)

Hope this helps.
 
Back
Top