Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lost Focus event only under certain circumstances

Status
Not open for further replies.

mpgalvin

Programmer
Feb 5, 2001
119
IE
I have 2 text boxes and button on a form (okay, so there's a lot more than that, but that's all that matters at the moment)

When the form is loaded, the first text box gets focus. If the user clicks or tabs to the second text box, I want to do something, so I put that into the LostFocus event. Works great.

Unfortunately, I don't want to perform the event if the user immediately clicks the button. So, in effect if the click event of the button causes the lost focus event of the text box, I want to ignore the lost focus event. I understand that the lost focus event comes first, but surely there's some way around this little problem.

Thanks
 
I don't see how you can do it without moving the guts of your code out of the lost focus event. How about just setting a variable in the lost focus event, and develop a general-purpose event handler which checks this variable among other things and acts accordingly. So you'd have
Code:
  txt1LostFocus = .T.
 DO EventHandler
    IF txt1LostFocus
        * lost focus code
        txt1LostFocus = .F.
    ELSE
        * do other things
    ENDIF

except that it'd be more complicated, possibly a DO CASE setup -- Dave
 
Maybe you just want the code that's now in the LostFocus event of textbox1 to be in the GotFocus event of textbox2.

Jim
 
Jim, that would be good if I only had the 2 textboxes, but I have more, so I can't be certain that the user won't click on a different control (I also can't run the LostFocus events more than once, as that would cause trouble).

No, I think Dave's "solution" ;-) is the best.
 
Sorry, Dave, but I'm not sure I'm on the same page as you as regards this idea.

I set the variable txt1LostFocus in the lostfocus event of the control. Where do I DO EVENTHANDLER then? And even if that works, that variable is going to be set when I click on the button anyway.

No, I've confused myself more than ever now.
 
Here are a couple of suggestions for you. Either of these may or may not work, depending on exactly what code you need to execute in the LostFocus event.

Start by creating either a global variable or form property called DoEvent and initialize it to .T. You could then use the MouseMove event of the command button to set DoEvent to .F. and the MouseMove event of the form to set DoEvent to .T., which would have the effect of knowing where you are going to click before the click happens. If your button has a hotkey, you can use the form KeyPreview property with the KeyPress event to intercept the keypress and set DoEvent to .F. as well. Then put your LostFocus event code in an If DoEvent...EndIf clause.

The downfall of the above method is that Windows might not react fast enough if you move the mouse from the button to a text box quickly, especially if the box is right next to the button. The form may never see the mouse move over it, and the flag wouldn't be reset. To compensate, you might also want to put the form's MouseMove event into some or all of your text boxes.

Another way would be to move the LostFocus code to each of the GotFocus boxes with the following modification. Set a global variable or form property called "CameFrom" and set it to 1 in the GotFocus of the main text box. In the GotFocus event of all of the other text boxes, put this:

if CameFrom=1
(your code goes here)
endif
CameFrom=0

This might be easier if you create a text box class with this code in it and create the text boxes out of the class.
 
After re-reading my post, it might be a little to complex to understand as written. Here is a simplified way to implement my first suggestion (which I tried myself and worked great).

Create form property "DoEvent", set to .T.
Form MouseMove event: Thisform.DoEvent=.T.
Command button MouseMove event: Thisform.DoEvent=.F.
Text box LostFocus event: If Thisform.DoEvent...EndIf

If your command button caption is &quot;\<Cancel&quot;, then put these in as well:

Form KeyPreview property: .T.
Form KeyPress event:
If nKeyCode=46
thisform.DoEvent=.F.
EndIf

If you have any text boxes that are very close to the command button, or are running on a very slow computer, you might also want the text box's MouseMove event to be the same as the form's MouseMove event.
 
1. Create a property for the form called DoMyCode or something like that.

2. Put the code that you want executed when the user leaves the first textbox into a procedure or method called MyCode.

3. In the lostfocus event of the first textbox put this code:
Thisform.DoMyCode = .T.

4. Place this code in the gotfocus event of the second textbox but not the button:

IF ThisForm.DoMyCode = .T. THEN
DO MyCode --or-- Thisform.MyCode()
Thisform.DoMyCode = .F.
ENDIF

Paul
 
MPG,

I think there are several versions now on the thread which all say much the same thing with variations. Create a varible which is set when you lose focus. Then in whatever gains focus, gets clicked, or whatever you call a program which checks to see if this variable (or any of perhaps numereous others) is set, and if so does whatever is desired and then resets the variable for the next usage.

Personally I like the idea of encapsulating it into whatever controls you want to react to this. But you can simply have a program or procedure that is called each time.

Dave Dardinger
 
Hi mpgalvin,
I suggest the following..

1. Create a form variable.. cOldTxt1

2. In the got focus event of the TxtBox1, initialise the variable value to the default value of yur txtBox1.Value

Exampe: GotFocusEvent of TxtBox1
ThisForm.cOldTxt1 = This.Value

3. In the lostFocusEvent of TxtBox1
If TxtBox1.Value # ThisForm.cOldTxt1
DO MyCommands
ThisForm.cOldTxt1 = This.Value
ELSE
DONT DO ANYTHING
ENDIF

The above will solve your problem.. I hope :)

ramani :-9
(Subramanian.G)
FoxAcc
ramani_g@yahoo.com
 
Wow! Thanks for all the great replies. So many good ideas! I eventually went with chpicker's 'cos that was the only I could understand ;-)

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top