Setting focus

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jack Russell

    #1

    Setting focus

    I have a form containing a tab control
    I want focus set to a particular text box when the form opens.

    However doing this in the load form event
    If TabControl1.Sel ectedIndex = 0 Then
    If txtFileName.Can Focus Then
    txtFileName.Foc us()
    End If

    End If
    I get false from canfocus.

    I can do it fine in the tabcontrol.sele ctedindexchange d event so why
    can't I do it in load?

    Thanks
  • Tom Shelton

    #2
    Re: Setting focus

    On 2005-06-08, Jack Russell <jackr@norubbis h.tpg.com.au> wrote:[color=blue]
    > I have a form containing a tab control
    > I want focus set to a particular text box when the form opens.
    >
    > However doing this in the load form event
    > If TabControl1.Sel ectedIndex = 0 Then
    > If txtFileName.Can Focus Then
    > txtFileName.Foc us()
    > End If
    >
    > End If
    > I get false from canfocus.
    >
    > I can do it fine in the tabcontrol.sele ctedindexchange d event so why
    > can't I do it in load?
    >
    > Thanks[/color]

    Controls have not necessarily completed their initilization in the
    form's load event. A control to receive the focus, must be enabled,
    visible, and have a handle. You may want to move this code to the forms
    activated event - though, you will want to put a flag to make sure this
    only happens the first time the activated event fires.

    You might also make this happen if you do something like:

    Me.Visible = True
    Me.Refresh
    If txtFileName.Can Focus Then
    txtFileName.Foc us ()
    End If

    In your load event. This will force the form to become visible and
    redraw it self....

    --
    Tom Shelton [MVP]

    Comment

    • Jack Russell

      #3
      Re: Setting focus

      Tom Shelton wrote:[color=blue]
      > On 2005-06-08, Jack Russell <jackr@norubbis h.tpg.com.au> wrote:
      >[color=green]
      >>I have a form containing a tab control
      >>I want focus set to a particular text box when the form opens.
      >>
      >>However doing this in the load form event
      >> If TabControl1.Sel ectedIndex = 0 Then
      >> If txtFileName.Can Focus Then
      >> txtFileName.Foc us()
      >> End If
      >>
      >> End If
      >>I get false from canfocus.
      >>
      >>I can do it fine in the tabcontrol.sele ctedindexchange d event so why
      >>can't I do it in load?
      >>
      >>Thanks[/color]
      >
      >
      > Controls have not necessarily completed their initilization in the
      > form's load event. A control to receive the focus, must be enabled,
      > visible, and have a handle. You may want to move this code to the forms
      > activated event - though, you will want to put a flag to make sure this
      > only happens the first time the activated event fires.
      >
      > You might also make this happen if you do something like:
      >
      > Me.Visible = True
      > Me.Refresh
      > If txtFileName.Can Focus Then
      > txtFileName.Foc us ()
      > End If
      >
      > In your load event. This will force the form to become visible and
      > redraw it self....
      >[/color]
      Tom,

      Thanks,

      Had tried it in the activated event to no avail but your second
      suggestion worked thanks

      Jack

      Comment

      Working...