Solved

Hot Keys and Lost Focus

Posted on 2002-07-17
12
567 Views
Last Modified: 2008-02-26
Hi,

I have a problem in that I fire a lost focus event on several controls on my form, but if focus is lost using a hot-key instead of clicking another control then the event isn't fired.

Has anyone found a way around this problem?
0
Comment
Question by:nsmith080197
  • 3
  • 3
  • 2
  • +4
12 Comments
 

Expert Comment

by:Amitabh
Comment Utility
i have just tried it with textbox control having a lostfocus() and clicked a hotkey for a command button and its working perfectly well. the lostfocus is working.
could u plz give a bit of ur code which is giving problem.
0
 
LVL 2

Expert Comment

by:priya_pbk
Comment Utility
Try this:

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)

'suppose you are pressing F6
If KeyCode = 117 Then
    Text1_LostFocus
    SendKeys "{tab}"
 End If
End Sub

Private Sub Text1_LostFocus()
MsgBox "lost focus fired"
'<---your code goes here----->
End Sub


Hope this helps

-priya


0
 
LVL 4

Expert Comment

by:TigerZhao
Comment Utility
Call Text1_LostFocus Will Not truly Lost Focus,delete this line

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
'suppose you are pressing F6
    If KeyCode = 117 Then
        SendKeys "{tab}"
    End If
End Sub
0
 
LVL 2

Expert Comment

by:priya_pbk
Comment Utility
yup, typo error, forgot to comment it.

-priya
0
 
LVL 6

Expert Comment

by:pierrecampe
Comment Utility
>>I fire a lost focus event on several controls on my form
??? how do you fire a lost focus event ???
0
 
LVL 2

Expert Comment

by:priya_pbk
Comment Utility
>>??? how do you fire a lost focus event ???

Well, events fire, when that event occurs on a particular control,

For eg in this case:-

nsmith wanted to use hotkeys in such a way that when the user presses these hotkeys(here F6) the focus should move out of the control(here textbox), [which actually is an event ie TextBox_lostFocus()] by sendkeys "tab" [this acts as if the user has pressed a tab key]..simple!!

So we try to do it programatically for the user, therefore the end user can press F6 or tab or enter, which all in turn fire the Text1_LostFocus event of that Text box.

I hope this helped!!

-priya

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Expert Comment

by:Amitabh
Comment Utility
hotkeys are, what we generally refer, are the keys with an underline like there is a command button with a caption update with u underlined. now if you press alt+u you are clicking update button.

now what i do not understand, is a similar scenario with a textbox, because to move to or away from the text box, in both cases you can use tab or hotkey of the control where you want to move, respectively.
like if you are writing in the text box and now want to click update, simply press alt+u and text_lostfocus will fire. you can also use tab to move to a next control.

this is a little bit confusing with a textbox where i think you don't need a hotkey. becoz u press the hotkey of other control to move away from textbox and not of textbox itself. hope you understood the logic. i don't think you need a hotkey in this case.

cheers!!
0
 
LVL 2

Expert Comment

by:ventond
Comment Utility
The actual windows term for these keys are accelerators.

When you have an accelerator on a command button then the lost focus does indeed fire. However, if the accelerator is a menu item it does not. The difference being that the command button actually receives the focus, but a menu never does.
0
 
LVL 6

Accepted Solution

by:
pierrecampe earned 100 total points
Comment Utility
>>I fire a lost focus event on several controls on my form
??? how do you fire a lost focus event ???
there is a good reason i asked this, there is no way you can FIRE a lost focus event, a lost focus event GETS(sometimes) fired as the result of an action (by the user or by your program)
the 'sometimes' is the problem
here a good explanation from 'advanced visual basic' by the mandelbrot set:

The Events from Hell
Visual Basics GotFocus and LostFocus events have always been exasperating to Visual Basic programmers. They dont correspond to the normal KillFocus and SetFocus messages generated by Windows; they dont always execute in the order that you might expect; they are sometimes skipped entirely; and they can prove very troublesome when you use them for field-by-field validation.

If these two events have caused problems in previous versions of Visual Basic, you might legitimately ask why Microsoft didnt find a way of fixing their behavior in Visual Basic 5. The reason they were left alone has to do with Visual Basic being designed to protect the developer as much as possible from the underlying environment and to be reasonably GPF-proof. If GotFocus and LostFocus really did correspond to KillFocus and SetFocus, trying to do something like show a message box in a GotFocus or LostFocus event would be walking straight into GPF territory. It appears that the Visual Basic team made a decision to post these events rather than send them. Under the ever-useful principle of least astonishment, most programmers would prefer not to have events whose order is difficult to predict or that disappear entirely, but thats what we have to live with.

To demonstrate some of the problems with these two events, I wrote a simple application. This project contains a single window with two text box controls, an OK command button, and a Cancel command button. (See Figure 7-1.)



Figure 7-1 Simple interface screen hides events from hell

Both command buttons have an accelerator key; the OK buttons Default property is set to TRUE (that is, pressing the Enter key will click this button), and the Cancel buttons Cancel property is set to TRUE (that is, pressing the Esc key will click this button). The GotFocus and LostFocus events of all four controls contain a Debug.Print statement that will tell you (in the Immediate window) which event has been fired. In this manner, we can easily examine the order in which these events fire and understand some of the difficulties with using them.

When the applications window is initially displayed, focus is set to the first text box. The Immediate window shows the following:

Program initialization
txtBox1_GotFocus
Just tabbing from the first to the second text box shows the following events:

txtBox1_LostFocus
txtBox2_GotFocus
So far, everything is as expected. Now we can add some code to the LostFocus event of txtBox1 to simulate a crude validation of the contents of txtBox1, something like this:

Private Sub txtBox1_LostFocus

Debug.Print "txtBox1_LostFocus"
If Len(txtBox1.Text) > 0 Then
    txtBox1_SetFocus
End If

End Sub
Restarting the application and putting any value into txtBox1 followed by tabbing to txtBox2 again shows what looks like a perfectly normal event stream:

txtBox1_LostFocus
txtBox2_GotFocus
txtBox2_LostFocus
txtBox1_GotFocus
Normally, however, we want to inform the user if a window control contains anything invalid. So in our blissful ignorance, we add a MsgBox statement to the LostFocus event of txtBox1 to inform the user that somethings wrong:

Private Sub txtBox1_LostFocus

Debug.Print "txtBox1_LostFocus"
If Len(txtBox1.Text) > 0 Then
    MsgBox "txtBox1 must be empty!"
    txtBox1_SetFocus
End If

End Sub
Restarting the application and putting any value into txtBox1 followed by tabbing to txtBox2 shows the first strangeness. We can see that when the message box is displayed, txtBox2 never receives focusbut it does lose focus!

txtBox1_LostFocus
txtBox2_LostFocus
txtBox1_GotFocus
Now we can go further to investigate what happens when both text boxes happen to have invalid values. So we add the following code to the LostFocus event of txtBox2:

Private Sub txtBox2_LostFocus

Debug.Print "txtBox2_LostFocus"
If Len(txtBox2.Text) = 0 Then
    MsgBox "txtBox2 must not be empty!"
    txtBox2_SetFocus
End If

End Sub
Restarting the application and putting any value into txtBox1 followed by tabbing to txtBox2 leads to a program lockup! Because both text boxes contain what are considered to be invalid values, we see no GotFocus events but rather a continuous cascade of LostFocus events as each text box tries to claim focus in order to allow the user to change its invalid contents. This problem is a well-known one in Visual Basic, and a programmer usually gets caught by it only once.

At this point, removing the MsgBox statements completely only makes the situation worse. If you do try this, your program goes seriously sleepy-bye-bye. Because the MsgBox function no longer intervenes to give you some semblance of control over the event cascade, youre completely stuck. Whereas previously you could get access to the Task Manager to kill the hung process, you will now have to log out of Windows to regain control.

These are not the only peculiarities associated with these events. If we remove the validation code to prevent the application from hanging, we can look at the event stream when using the command buttons. Restart the application, and click the OK button. The Immediate window shows a normal event stream. Now do this again, but just press Enter to trigger the OK button rather than clicking on it. The Debug window shows quite clearly that the LostFocus event of txtBox1 is never triggered. Exactly the same thing happens if you use the OK buttons accelerator key (Alt+O)no LostFocus event is triggered. Although in the real world you might not be too worried if the Cancel button swallows a controls LostFocus event, its a bit more serious when you want validation to occur when the user presses OK.

There are several more or less satisfactory methods of overcoming the problems with these two events. Possibly the best strategy is to ignore them altogether and instead use a tool such as MsgBlaster or SpyWorks to intercept the Windows messages that correspond to these events (SetFocus and KillFocus). This makes performing activities such as field-by-field validation relatively trivial.

Some Final Thoughts Never rely on GotFocus and LostFocus events actually occurring or occurring in the order you expect.
0
 
LVL 6

Expert Comment

by:pierrecampe
Comment Utility
in vb6 MS has tried to remedy this with the 'validate' event, it does a good job unless there is a default button,then that event is not fired either if you pres enter
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Hi nsmith,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept pierrecampe's comment(s) as an answer.

nsmith, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:Computer101
Comment Utility
Comment from expert accepted as answer

Computer101
E-E Admin
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now