Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2006

Tip: Looking for answers? Try searching our database.

Setting focus

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Howard Kaikow - 26 Jun 2006 11:06 GMT
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?
Nat - 26 Jun 2006 12:49 GMT
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.
Herfried K. Wagner [MVP] - 26 Jun 2006 14:39 GMT
"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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.