Results 1 to 8 of 8

Thread: [RESOLVED] Focus question, hopefully simple

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [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)
    Attached Files Attached Files
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Focus question, hopefully simple

    the reason is thus:

    VB Code:
    1. Private Sub Text1_Validate(Cancel As Boolean)
    2.     ' This Sub is triggered by, but happens before the focus moves to
    3.     ' the next Enabled control (Command2), text1 still has focus
    4.     Command1.Enabled = True
    5.     Command1.SetFocus
    6. End Sub ' Focus carries on to where you originally directed it (command2)

    not sure how to get round it, focus events always annoy me

  3. #3
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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:
    1. Private Sub Command2_Click()
    2.     Command1.SetFocus
    3. End Sub

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Focus question, hopefully simple

    you'd still have the same problem if the user tabbed out of the textbox though

  5. #5
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Focus question, hopefully simple

    The complete version would be..
    VB Code:
    1. Private Dataok As Boolean
    2.  
    3. Private Sub Command2_Click()
    4.     If Dataok Then Command1.SetFocus
    5. End Sub
    6.  
    7. Private Sub Text1_GotFocus()
    8.     Command1.Enabled = False
    9. End Sub
    10.  
    11. Private Sub Text1_Validate(Cancel As Boolean)
    12.     If  Text1.Text <> vbNullString Then
    13.         Dataok = True
    14.         Command1.Enabled = True
    15.     Else
    16.         Dataok = False
    17.     End If
    18. End Sub
    Last edited by jcis; Feb 18th, 2006 at 06:23 AM.

  6. #6

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    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:
    1. Private Sub Text1_GotFocus()
    2.     Command1.Enabled = False
    3. End Sub
    4.  
    5. Private Sub Text1_LostFocus()
    6.     Command1.SetFocus
    7. End Sub
    8.  
    9. Private Sub Text1_Validate(Cancel As Boolean)
    10.     Dim i As Long
    11.     Command1.Enabled = True
    12. End Sub
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  7. #7
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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:
    1. Private Sub Text1_GotFocus()
    2.     Command1.Enabled = False
    3. End Sub
    4.  
    5. Private Sub Text1_LostFocus()
    6.     Command1.SetFocus
    7. End Sub
    8.  
    9. Private Sub Text1_Validate(Cancel As Boolean)
    10.     Dim i As Long
    11.     Command1.Enabled = True
    12. End Sub
    But, that won't work if the text in text1 is not validated as ok, that's why i added a boolean

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    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.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width