Solved

LostFocus event does not work when pressing the menubar

Posted on 2002-05-29
15
313 Views
Last Modified: 2008-02-01
Here is an example to show my problem.

1. Open a new project
2. Add a 1st textbox control named Text1
3. Add a 2nd textbox control named Text2
3. Add a menu item by menu editor and named it Test and mnuTest

======================================================
Option Explicit

Private Sub mnuTest_Click()
    MsgBox "Test Menu is clicked"
End Sub

Private Sub Text1_LostFocus()
    MsgBox "Text1_LostFocus"
End Sub

Private Sub Text2_LostFocus()
    MsgBox "Text2_LostFocus"
End Sub

======================================================

My question is:
    How can the LostFocus event be run when click on the menubar? For some reason, the LostFocus event should be run in my project. I'm pleased to have any idea / workaround.
0
Comment
Question by:VincentWong
  • 6
  • 3
  • 3
  • +2
15 Comments
 
LVL 1

Expert Comment

by:zzconsumer
ID: 7041264
I could reproduce this problem. You might give another control the focus in the top level of you menu, e.g.

Private Sub mnuFile_Click
Text1.SetFocus
End Sub

This is not one of the best methods, but it works.
0
 
LVL 2

Author Comment

by:VincentWong
ID: 7041281
mmm... I've suggested this workaround to my team leader before. But it has been turned down already. Any more solution for it?
0
 
LVL 2

Expert Comment

by:priya_pbk
ID: 7041289
'try this

Private Sub mnutext_Click()
  MsgBox "Test Menu is clicked"
  Text1_LostFocus
End Sub

Private Sub Text1_LostFocus()
   MsgBox "Text1_LostFocus"
End Sub

Private Sub Text2_LostFocus()
   MsgBox "Text2_LostFocus"
End Sub

'is this what you want! Hope this helps

-priya
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 7041295
Try this to call the LostFocus event of the active control:

Private Sub mnutext_Click()
 dim strControl as string
 strControl = ActiveControl.Name

on error resume next
 callbyname me , strControl & "_LostFocus", vbMethod

End Sub

If the ActiveControl.Name doesn't return the good value, then you might do this:

Private lstrActiveControl as string


Private Sub mnutext_Click()
 MsgBox "Test Menu is clicked"

on error resume next
 callbyname me , lstrActiveControl & "_LostFocus", vbMethod
End Sub

Private Sub Text1_LostFocus()
  MsgBox "Text1_LostFocus"
End Sub

Private Sub Text2_LostFocus()
  MsgBox "Text2_LostFocus"
End Sub

Private Sub Text1_GotFocus()
  lstrActiveControl = ActiveControl.Name
End Sub

Private Sub Text2_GotFocus()
  lstrActiveControl = ActiveControl.Name
End Sub


CHeers
0
 
LVL 2

Author Comment

by:VincentWong
ID: 7041296
Thx for your suggestion first. But I've got 20 sth textbox and other controls in the Form. And I only want the last accessed control to run the LostFocus event.

Good begining with at least 2 ways to do so.

Any better idea?
0
 
LVL 2

Author Comment

by:VincentWong
ID: 7041308
Hi angelIII, I have tried your code. But the LostFocus does not be called actually. So I comment out 'on error resume next' to have a try. It seems that the 'callbyname' method can't be run. Any more setting/reference to include for using it?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7041311
I think angelIII has the best answer, it appears that when you click a menu item the current control does not lose the focus so no events are raised, neither the _Validate event which is fired before the _LostFocus event (which sometimes doesn't get fired anyway) occur. In this case the only logical method to force this is using .ActiveControl as suggested.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Expert Comment

by:priya_pbk
ID: 7041312
yeah, its a good suggestion given by angelIII. I tried the code, but gives me an error at the following line, ie where I am calling the procedure:

>>callbyname me , lstrActiveControl & "_LostFocus", vbMethod

Error .."Object doesn't support this property or method"

why so?just curious!!

-priya


0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7041320
The problem is that vbMethod requires a Function not a Sub and as the declaration for _LostFocus is a Sub not a Function it fails. Perhaps there is a way around this, I will have a look and see.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7041324
sorry.... The LostFocus events need to be PUBLIC instead of PRIVATE so they can be called by the CallByName function...

The "on error resume next" was intended for those controls that do not have this LostFocus event.

Cheers
0
 
LVL 2

Author Comment

by:VincentWong
ID: 7041325
Just share more about the Funny LostFocus event with you all.

1. Open a new project
2. Add a 1st textbox control named Text1
3. Add a 2nd textbox control named Text2
****************************************
4. Add one more commandButton name Command1
****************************************
5. Add a menu item by menu editor and named it Test and mnuTest

======================================================
Option Explicit

Private Sub Command1_GotFocus()
   Msgbox "Command1_GotFocus"
End Sub

Private Sub mnuTest_Click()
   command1.SetFocus
   MsgBox "Test Menu is clicked"
End Sub

Private Sub Text1_LostFocus()
   MsgBox "Text1_LostFocus"
End Sub

Private Sub Text2_LostFocus()
   MsgBox "Text2_LostFocus"
End Sub

======================================================

Then the LostFocus event of the textbox would fail again.


0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7041330
No way around that so far, how about another option?

If you create a dummy control such as a textbox that you position off the form to one side you can use this to receive the focus when you click the menu. This will fire the _LostFocus event of the activecontrol as you want and you can always setfocus back to the control that had it after you have finished your code in the menu click event:

Private Sub mnuTest_Click()
Dim strControl As String
strControl = ActiveControl.Name

On Error Resume Next
Dummy.SetFocus
MsgBox "here is some code"
Me.Controls(strControl).SetFocus
End Sub

Here Dummy is a textbox control with .Left = -2000 to keep it out of view.
0
 
LVL 2

Expert Comment

by:priya_pbk
ID: 7041347
yes, I think angelIII  has the best suggestion. Change the procedure's from Private to Public. Works for me!, even with command button or text box, whatever.
0
 
LVL 2

Author Comment

by:VincentWong
ID: 7041357
Hi TimCottee, I have tried as your way.

Private Sub mnuTest_Click()
    Dummy.SetFocus
    MsgBox "here is some code"
End Sub

(Let Dummy as a control)

However, if there exists a msgbox after the SetFocus command, the LostFocus event would fail again.

More information, I'm using VB6 with SP5.
0
 
LVL 2

Author Comment

by:VincentWong
ID: 7041377
It works, angelIII. thx a lots
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

759 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

24 Experts available now in Live!

Get 1:1 Help Now