1 Attachment(s)
[RESOLVED] Focus question, hopefully simple
I have a textbox on top of the form and 2 underlying command buttons. When the textbox gets the focus, the first command button is disabled. When the text has been validated, this button is enabled and is supposed to receive the focus, but the other button gets the focus instead. What have I done wrong? (See attachment)
Re: Focus question, hopefully simple
the reason is thus:
VB Code:
Private Sub Text1_Validate(Cancel As Boolean)
' This Sub is triggered by, but happens before the focus moves to
' the next Enabled control (Command2), text1 still has focus
Command1.Enabled = True
Command1.SetFocus
End Sub ' Focus carries on to where you originally directed it (command2)
not sure how to get round it, focus events always annoy me :)
Re: Focus question, hopefully simple
It happends because the validate event occurs before the click, then the solution would be to Set Focus to Command1 in the click event of Command2
VB Code:
Private Sub Command2_Click()
Command1.SetFocus
End Sub
Re: Focus question, hopefully simple
you'd still have the same problem if the user tabbed out of the textbox though
Re: Focus question, hopefully simple
The complete version would be..
VB Code:
Private Dataok As Boolean
Private Sub Command2_Click()
If Dataok Then Command1.SetFocus
End Sub
Private Sub Text1_GotFocus()
Command1.Enabled = False
End Sub
Private Sub Text1_Validate(Cancel As Boolean)
If Text1.Text <> vbNullString Then
Dataok = True
Command1.Enabled = True
Else
Dataok = False
End If
End Sub
Re: Focus question, hopefully simple
I've found an easier solution: placing the reenabling code line in the textbox lostfocus event, as follows.
VB Code:
Private Sub Text1_GotFocus()
Command1.Enabled = False
End Sub
Private Sub Text1_LostFocus()
Command1.SetFocus
End Sub
Private Sub Text1_Validate(Cancel As Boolean)
Dim i As Long
Command1.Enabled = True
End Sub
Re: Focus question, hopefully simple
Quote:
Originally Posted by krtxmrtz
I've found an easier solution: placing the reenabling code line in the textbox lostfocus event, as follows.
VB Code:
Private Sub Text1_GotFocus()
Command1.Enabled = False
End Sub
Private Sub Text1_LostFocus()
Command1.SetFocus
End Sub
Private Sub Text1_Validate(Cancel As Boolean)
Dim i As Long
Command1.Enabled = True
End Sub
But, that won't work if the text in text1 is not validated as ok, that's why i added a boolean :)
Re: Focus question, hopefully simple
Quote:
Originally Posted by jcis
But, that won't work if the text in text1 is not validated as ok, that's why i added a boolean :)
I meant, place the command2.setfocus (not command2.enabled=true, my mistake) in the textbox.lostfocus event. At any rate the code I've posted works, try it.