?
Solved

LostFocus event does not work when pressing the menubar

Posted on 2002-05-29
15
Medium Priority
?
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 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
 
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 143

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

800 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