Results 1 to 21 of 21

Thread: How to prevent app to execute next line code when setfocus failed?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    How to prevent app to execute next line code when setfocus failed?

    Hi,

    I have two textbox control, named "Textbox1" and "Textbox2" and one Command Button named "Command1.
    Focus currently on "Textbox1".

    In Command1 click event i have code something like this:

    Private Sub Command1_Click()
    Textbox2.Setfocus
    etc...
    etc...
    End Sub

    On Texbox1 lostfocus() event i have some condition before it can lostfocus.

    My question is how to prevent execute next line code on command1_click event when Textbox2.setfocus failed (Textbox1_lostfocus() event conditon doesn't meet) ?

    Thanks.

  2. #2
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    Re: How to prevent app to execute next line code when setfocus failed?

    many options here, but i think a variable is what really is important in this situation.
    VB Code:
    1. 'here is an example
    2. Private OkToProceed As Boolean
    3.  
    4. Private Sub Command1_Click()
    5.  
    6.     If OkToProceed Then
    7.         Text2.Enabled = True 'enable the 2nd text
    8.         Text1.Locked = True 'lock the first one
    9.     Else
    10.         Exit Sub
    11.     End If
    12.  
    13. End Sub
    14.  
    15. Private Sub Text1_LostFocus()
    16.  
    17.     If IsNumeric(Text1) Then
    18.         OkToProceed = True
    19.     End If
    20.  
    21. End Sub
    Last edited by Billy Conner; Sep 28th, 2006 at 09:42 PM. Reason: ' something

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: How to prevent app to execute next line code when setfocus failed?

    I'm trying it now, but can you give me another option so i can see what method is best for what i need
    Last edited by barianto; Sep 28th, 2006 at 10:07 PM.

  4. #4
    Lively Member okosv's Avatar
    Join Date
    Sep 2006
    Posts
    95

    Re: How to prevent app to execute next line code when setfocus failed?

    use On Error Goto statement

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: How to prevent app to execute next line code when setfocus failed?

    Hi,

    okosv, can you give me sample?

    Billy, okosv, i redefined what i need. It actually combobox instead of textbox, sorry for that. If you guys got time please see attachment.

    If i'm not mistaken when Combo2_DropDown event fired all code in it got executed first than combo2_lostfocus event get fired. This is where the problem come.
    Attached Files Attached Files
    Last edited by barianto; Sep 28th, 2006 at 11:49 PM.

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How to prevent app to execute next line code when setfocus failed?

    I looked at your form and from what I can gather from what you've said, it's working alright.. If Combo1 is blank when it loses focus, then put focus back onto it. If it's not blank, then let the user interact with Combo2. It worked fine for me -- is that what you want? If not, then I don't understand what you're asking for, so if you could, try explaining it again.
    Like Archer? Check out some Sterling Archer quotes.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: How to prevent app to execute next line code when setfocus failed?

    Hi, kows

    It's "seem" to working alright

    Try do this, type anything on combo1 and then click on combo2 dropdown arrow. Msgbox "Ok To Proceed Should be pop up since OkToProceed value now "true", but it doesn't. Click combo2 dropdown arrow ones again, voila the message box pop up.

    Instead that try do this, type anything on combo1, click on combo2 "text area" and then click on combo2 dropdown arrow. Msgbox "Ok To Proceed will pop up as expected.

    Thanks.

  8. #8
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How to prevent app to execute next line code when setfocus failed?

    Yes, I noticed this when I originally replied, and that's how Focus events work. If the focus of an object is on something, like a Textbox, it will have a blinking cursor inside of it. Until you click on another Textbox the focus will still be on the original Textbox, and because clicking on the drop down arrow doesn't move the cursor, it won't trigger the LostFocus event until you put the focus onto something else.

    In your code, though, on Combo2's DropDown event you make a SetFocus. However, the reason it's acting the way it is (unresponsive to the newly assigned focus) is probably because you changed the focus programmatically and it won't fire the LostFocus event unless you do it manually... so, as a work around, you can just call the LostFocus event of Combo1, and this will fix your problem. It can get a little messy if you're doing this over a large amount of combo boxes, but either way, it would still work the way you want it to:

    VB Code:
    1. Private Sub Combo2_DropDown()
    2.     Combo2.SetFocus
    3.     Call Combo1_LostFocus
    4.     If OkToProceed Then
    5.        MsgBox "Ok To Proceed"
    6.     End If
    7. End Sub

    Let me know if that solves your problem.
    Like Archer? Check out some Sterling Archer quotes.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: How to prevent app to execute next line code when setfocus failed?

    Hi, kows
    Thanks for reply.

    I have so many combobox with situation like this. It's difficult to tell what combobox lostfocus event should i call when i have 3 or more combobox like this. Like you said it's going to messy

    Regarding your explanation about lostfocus behaviour, i noticed when combo1 text is empty and i click combo2 dropdown arrow. Combo1 lostfocus trigger as expected. And as i watched it closely, when ic click Combo2 Dropdown arrow cursor temporaryly moved to combo2 text area
    Last edited by barianto; Sep 29th, 2006 at 02:28 AM.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: How to prevent app to execute next line code when setfocus failed?

    Bump!!!

  11. #11
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How to prevent app to execute next line code when setfocus failed?

    ..? what's your problem?

    It will always temporarily move to the Combo2 text area, but then move back. Unless you completely disable Combo2, I don't believe you can do anything to prevent it.
    Like Archer? Check out some Sterling Archer quotes.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: How to prevent app to execute next line code when setfocus failed?

    Hi, kows

    Good to hear from you.
    I still hoping that i can do "something" to prevent it

  13. #13
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How to prevent app to execute next line code when setfocus failed?

    As far as I know -- you can't. If the field is enabled and a user puts focus on it, it will have focus until you change it, even if you change it as soon as that item gets focus. If you don't want the user to be able to mess with it, use Combo2.Enabled=False, and when you want to allow them to use it, re-enable it. Or, you could always try making a mask of some sort over top of it (like a PictureBox, that has a picture of the ComboBox inside of it) to make it seem like the ComboBox was usable, and just set the PictureBox invisible when you want to be able to use the second ComboBox.
    Like Archer? Check out some Sterling Archer quotes.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: How to prevent app to execute next line code when setfocus failed?

    Hi, kows

    Sorry to mislead you. Much thing i don't discovered here since my project is big.
    But last suggestion you made is not gonna solve my problem.
    The only way to solve my problem is when dropdown arrow of combo box get clicked lostfocus on other control who currently got focus executed first. Than all code in event click combo dropdown can be executed

  15. #15
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: How to prevent app to execute next line code when setfocus failed?

    Use the Validate event instead.

    VB Code:
    1. Private Sub Combo1_Validate(Cancel As Boolean)
    2.     Cancel = Combo1.ListIndex = -1
    3. End Sub

    Combo1 will not lose focus to Combo2 if the Validate event sets the Cancel argument to True

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: How to prevent app to execute next line code when setfocus failed?

    Hi brucevde,

    Actually combobox is just dummy, when dropdown arrow was clicked instead of dropdoen box frame will popup. Please see attachment on my previous post

  17. #17
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: How to prevent app to execute next line code when setfocus failed?

    Frame? No where did you mention anything about a Frame, including the attachment you posted earlier.

    First you talked about a problem with TextBoxes and Command buttons. Then it became about ComboBoxes.

    Get back to us when you know what you want help with and can describe it more clearly.

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: How to prevent app to execute next line code when setfocus failed?

    Hi brucevde

    Sorry for the inconvenience .
    Yes i was confusing with what to ask at first. But Please see post #5 in this thread. There is attachment also.

    Thanks

  19. #19
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: How to prevent app to execute next line code when setfocus failed?

    I did see your attachement and I posted a suggestion to use the Validate event. Again, the Combo2_DropDown event will not fire if the Cancel argument of the Combo1_Validate event is set to True. Remove all code in your sample project and use this code (previously I used ListIndex)

    VB Code:
    1. Private Sub Combo1_Validate(Cancel As Boolean)
    2.     Cancel = Combo1.Text = ""
    3. End Sub

    What about this. Set Combo2.Enabled to False until something has been entered/selected in Combo1.

    VB Code:
    1. Private Sub Combo1_Validate(Cancel As Boolean)
    2.     Combo2.Enabled = Len(Combo1.Text) > 0
    3. End Sub
    Last edited by brucevde; Oct 3rd, 2006 at 01:31 AM.

  20. #20
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How to prevent app to execute next line code when setfocus failed?

    i already suggested that. he doesn't want to disable the second combobox.
    Like Archer? Check out some Sterling Archer quotes.

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    145

    Re: How to prevent app to execute next line code when setfocus failed?

    Hi brucevde,

    Validate event as suggest work the way i want. Right now i'm testing it try to find anything that wasn't right -- Hope i don't find it though --.

    Yes, as kows say i don't want to disable combo number 2

    Thanks
    Last edited by barianto; Oct 3rd, 2006 at 03:21 AM.

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