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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Intersting Problem LOSTFOCUS not fired...? 4

Status
Not open for further replies.

nomi2000

ISP
Feb 15, 2001
676
0
0
CA
Dear fellow Programmers
I am facing an intersting prob although i have a solution but still want to share this with
you so i might got any other better soltn for this
now the prob is that say i have a TEXTBOX,a LABEL and a COMMAND BUTTON (With Access key ALT+S)
i have done some calcuation at the LOSTFOCUS of the TEXTBOX which i am showing in LABEL.
now the prob is that when i pressed ALT+S at textbox the LOSTFOCUS of the textbox
not fired so my calculation also not done,i can't use any other event of the TEXTBOX for some
reasons..i am wondering why LOST FOCUS not fired when i pressed ALT+S ,the solution i have is
to also write calculation code in the CLICK event of the BUTTON,that will solve my prob but
i want to hear if i can get why this happening?
Regards
Nouman
 
I would suggest that the event code for the command button is being executed before the lost focus event on the textbox. Try putting DoEvents in the command button event (at the top of it),
 
Dear mmilan
thanx for ur help ur hekp is worthless i have done it
thanx very much
Regards
Nouman
 
mmilan or any one thee is one q in addition,if i could somehow stop firing LOSTFOUCS when i click from Mouse on TEXTBOX and then click somewhere else?
Regards
Nouman
 

TO nomi2000:

You cannot stop the LOSTFOCUS when you go elsewhere. Some how the active form (window) needs to know that the next click/keypresses are NOT to be accepted by that form (window) but infact must be directed elsewhere.

As for catching the ALT+S key: Try setting the form's KEYPREVIEW to true. This will normally capture the keystrokes before sending it to the textbox.

Hope this helps. --MiggyD

Never be afraid to try something new. Remember that amateurs built the Ark. Professionals built the Titanic.
 
A trick I sometimes use to stop events from firing when I don't want them is to have a form level boolean variable (true/ false).

Then, in the event procedure I want control over, the first thing I do is test the state of this variable, and if it is true, use Exit Sub to abandon the event.

This effectively gives you the ability to turn the events on or off simply by setting the variable.

 
Please research the Validate event. It is the proper event to use for handling lost focus types of problems... Microsoft introduced this event to solve EXACTLY the problems you are discussing...
 
yes mmilan
thats the thing which i also do :) but does VB doesn't support this simple thing to stop an event to fire..i think i have to write keyboard hook to do this but know its difficulte to implements and most importantly how ?
Regards
Nouman
 
The validate event will work nicely for the three control scenario described in the original post, but bear in mind that this is a hypothetical situation.

The problem the user is actually experiencing is likely to involve a good many more controls. I do wonder if validate might be a little cumbersome to handle - unless of course he only wants the code to run when the command button is clciked, but I can't beleive that's the case or he'd just have stuck it in the click event.

I'd still put a DoEvents call at the top of the click event, but then that's just me, it's a free country, and I have just as much right to be wrong as the next guy ;o)
 
Der Mmilan
well yes the DoEvents works fine to me now the LOSTFOCUS always fired and i alwaz get teh refresh calculation ,
now there is a q in conjunciton with my original post which i am experiencing the problem is that when i there is some correct data in textbox and then i click the Cross button of Form to actually close the form without doing anything the LOSTFOUCS fired i don't want to check calculation at this instance ,perhaps i don't want LOSTFOUCS to not fired in this scenirio..
Regards
Nouman
 
In that case, I'd use a form level boolean variable, as discussed earlier...

If that doesn't get around the problem, let me know.

mmilan
 
As a general rule, you NEVER want to trigger any action
off of a LostFocus event. It's so easy for it to trigger
at inappropiate times. Instead, just add another Command button and have done with it.
 
Dear Mmilan
the prob (question) which i asked in conjunction is also a very frustating one and i got the problem why this is happening the prob is that when i clicked at the Textbox and then clicked at the cross button of the FORM to close the form then LOSTFOCUS of the textbox fired first i can't figure it out why this happening but then i got it that its due to FORM Keypreiview=true property so i made it false and the lost focus wont fired but still the problem of not firing the LOSTFOUCS event in case when i just clicked from textbox to other place on the FORM still remains ,but yes let me see the boolean variable techq,i think it should works
dear THOMASG what u are telling ? do u mean i have to make another command button for stopping the lostfocus? first i don't want any other control and if this is the case then how do this? will you explain little more thanx
Regards
Nouman
 
Try putting a DoEvents call into the lostfocus event at the top. Then make the lost focus code only work if a form level boolean is set to false. In the Unload event for the form, set the boolean to true.

That should work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top