LostFocus event does not work when pressing the menubar

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.
LVL 2
VincentWongAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
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
 
zzconsumerCommented:
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
 
VincentWongAuthor Commented:
mmm... I've suggested this workaround to my team leader before. But it has been turned down already. Any more solution for it?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
priya_pbkCommented:
'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
 
VincentWongAuthor Commented:
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
 
VincentWongAuthor Commented:
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
 
TimCotteeHead of Software ServicesCommented:
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
 
priya_pbkCommented:
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
 
TimCotteeHead of Software ServicesCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
VincentWongAuthor Commented:
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
 
TimCotteeHead of Software ServicesCommented:
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
 
priya_pbkCommented:
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
 
VincentWongAuthor Commented:
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
 
VincentWongAuthor Commented:
It works, angelIII. thx a lots
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.