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
> I have the following as the only statement in a Form's Load event:
>
[quoted text clipped - 3 lines]
>
> How to fix?
Howard Kaikow - 26 Jun 2006 23:44 GMT
> Hello Howard,
>
[quoted text clipped - 3 lines]
> 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.
"Howard Kaikow" <kaikow@standards.com> schrieb:
>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.

Signature
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Howard Kaikow - 26 Jun 2006 23:49 GMT
> 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.
Howard Kaikow - 27 Jun 2006 11:30 GMT
> "Howard Kaikow" <kaikow@standards.com> schrieb:
> >I have the following as the only statement in a Form's Load event:
[quoted text clipped - 7 lines]
> 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?
kelly.pearson@ct.gov - 27 Jun 2006 17:19 GMT
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.
> > "Howard Kaikow" <kaikow@standards.com> schrieb:
> > >I have the following as the only statement in a Form's Load event:
[quoted text clipped - 11 lines]
> Your suggestion works, but where is it documented that the action is delayed
> until the control becomes visible?