0

I have a breakpoint on the first line of a TextBox1.GotFocus event function.

When I call TextBox1.SetFocus elsewhere, the GotFocus breakpoint is never hit. Why?

Code in calling function: Text1.SetFocus

Private Sub Text1_GotFocus()

   // code here

End Sub
CJ7
  • 22,579
  • 65
  • 193
  • 321
  • maybe your textbox already has the focus ? (could you post the code of the gotfocus event and where setfocus is called, also try setting a breakpoint on the setfocus call to see if it's actually called) – Hrqls Nov 26 '12 at 06:27
  • see edit. I have stepped through the code to see that the setfocus call is being made. It steps straight on to the next line without going into gotfocus. – CJ7 Nov 26 '12 at 06:37
  • are you sure text1 doesn't already have the focus ? try to set the focus to something else first before setting it to text1 – Hrqls Nov 26 '12 at 06:39
  • I wonder if **GotFocus** is only called on user invention and not through automation from **SetFocus**, does the event fire when you manually click in the field? – Matt Donnan Nov 26 '12 at 11:33
  • @MattDonnan : it does fire, check the small program i posted below – Hrqls Nov 27 '12 at 08:11

1 Answers1

0

a little program to show what i mean.

run the program and click the form (the caption remains 1), click on Text2 to give it the focus, and click the form again (caption changes to 2)

then do the same while Text2.SetFocus is uncommented in Form_Click

here is the code :

'1 form with
'    textbox : name=Text1    tabindex=0
'    textbox : name=Text2    tabindex=1
Option Explicit

Private Sub Form_Click()
  'uncomment the following line to make it work
'  Text2.SetFocus
  'with just the following call this wont work
  Text1.SetFocus
End Sub

Private Sub Text1_GotFocus()
  'increase the number in the form caption to show text1 got the focus again
  Caption = CStr(Val(Caption) + 1)
End Sub

when the program starts Text1 gets the focus (tabindex=0), so the form caption changes to 1 when you click the form nothing changes because Text1 already has the focus and ddn't "get" it when you first click on Text2 and then click the form the form caption increases

by uncommenting the line with Text2.SetFocus you let the program always move the focus to Text2 (if it's not already there), before moving the focus to Text1, so Text1 will always "get" the focus anew

be careful though! as giving the focus to another control first might spawn some new events which you might not want!

Hrqls
  • 2,944
  • 4
  • 34
  • 54