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) ?
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.
Last edited by barianto; Sep 28th, 2006 at 11:49 PM.
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.
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.
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:
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.
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.
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.
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
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:
Private Sub Combo1_Validate(Cancel As Boolean)
Cancel = Combo1.Text = ""
End Sub
What about this. Set Combo2.Enabled to False until something has been entered/selected in Combo1.
VB Code:
Private Sub Combo1_Validate(Cancel As Boolean)
Combo2.Enabled = Len(Combo1.Text) > 0
End Sub
Last edited by brucevde; Oct 3rd, 2006 at 01:31 AM.