We help IT Professionals succeed at work.

MDIForms and gotFocus equivalent???

siegi
siegi asked
on
Medium Priority
366 Views
Last Modified: 2008-02-01
Hi all,

I'm wondering if there is an equivalent to the standard gotFocus Event on regular forms in a MDIForm?  I'm running a menu under this MDIForm, certain choices of which I don't want to be active until something else is entered and finished.  I also want to leave the option of not closing this particular form (that needs to be completed) open.  The lostFocus Event on the MDIChild form is not triggered if you click over to the menu of the parent MDI form.  Any ideas of what I should do?

I hope this is clear enough??
Cheers,
-siegi
Comment
Watch Question

Senior DBA
CERTIFIED EXPERT
Commented:
Try using the Activate and Deactivate events for determinig whether a given child form is active.

As for the problem of the active parent MDI form, if there is one open child window, it is still the default ActiveForm of the parent.  Only if another child form becomes active does the first child form become inactive.  If you move to a non-MDI form though, the Parent MDI form received the Activate / Deactivate events.

Commented:
Here is the trick to emulate GotFocus for MDI form - use Picturebox to receive focus.
It assumes your MDIForm doesn't have other controls that can get focus.

' MDIForm, add picturebox (Height=0), and two top-level menus
'    mnu1  (Caption = "Show_Form")
'    mnu2  (Caption = "Certain_Choices")
'
Option Explicit

Private Sub my_GotFocus()
    mnu2.Enabled = True
End Sub

Private Sub Picture1_LostFocus()
    mnu2.Enabled = False
End Sub

Private Sub Picture1_GotFocus()
   Dim i As Integer
   
   ' check MDI children
   For i = 0 To Forms.Count - 1
       If Not Forms(i) Is Me Then
           If Forms(i).MDIChild = True Then
               Exit Sub
           End If
       End If
   Next
   ' all MDI children are closed, 'raise' GotFocus event
   my_GotFocus
End Sub

Private Sub mnu1_Click()
    Dim x As Form1
    Set x = New Form1
    x.Show
End Sub

Author

Commented:
bhess1: thanx that's my problem exactly...

ameba: thanx.. I tried it and followed it through, but what I'm trying to do is a bit different (unless I'm misunderstanding the potentials of this).

-I have two data entry forms (MDIChilds)...  
-I need data in both before I can do a calculate procedure...
-Depending on this calculate, certain Results buttons in my MDIParent form should become activated(enabled).
Problem: I want to keep both data entry forms open while realizing that enough data is entered to do a calculate and enable the buttons.  I can't enable the buttons in the calculate command because thats logicaly off and I don't have anything to invoke it until I Close my data forms or loose focus on them (here is where I hope I'm missing something and you guys can help out).
Prob con't: Because the form doesn't actually loose focus as you click on the MDIparent menu, I can't invoke the calculate or the menu button enabled portion of my code.

Any ideas?  I think ameba is close but not quite what I had in mind.

-siegi

Author

Commented:
bhess1: thanx that's my problem exactly...

ameba: thanx.. I tried it and followed it through, but what I'm trying to do is a bit different (unless I'm misunderstanding the potentials of this).

-I have two data entry forms (MDIChilds)...  
-I need data in both before I can do a calculate procedure...
-Depending on this calculate, certain Results buttons in my MDIParent form should become activated(enabled).
Problem: I want to keep both data entry forms open while realizing that enough data is entered to do a calculate and enable the buttons.  I can't enable the buttons in the calculate command because thats logicaly off and I don't have anything to invoke it until I Close my data forms or loose focus on them (here is where I hope I'm missing something and you guys can help out).
Prob con't: Because the form doesn't actually loose focus as you click on the MDIparent menu, I can't invoke the calculate or the menu button enabled portion of my code.

Any ideas?  I think ameba is close but not quite what I had in mind.

-siegi
Brendt HessSenior DBA
CERTIFIED EXPERT

Commented:
I would probably set up a subroutine that would enable the buttons based on the values I define in a UDT like this (example):

Type DataEnteredFlags
   Name As Boolean
   Addr As Boolean
   ShipDate As Boolean
   Billing As Boolean
   PayMethod As Boolean
End Type

Dim DataEntered as DataEnteredFlags

When I start a new entry, reset the flags:

Sub ResetFlags
   With DataEntered
      .Name = False
      .Addr = False
      .ShipDate = False
      .Billing = False
      .PayMethod = False
   End With
End Sub

As I enter the data, I would set the flags, e.g.

Sub txtCustomerName_LostFocus ()
   DataEntered.Name = (Trim(txtCustomerName.Value) > "")
   CheckValidData
End Sub

CheckValidData would look to see if all of the flags were set to true, e.g.

Sub CheckValidData ()
   With DataEntered
      cmdBookIt.Enabled = .Name AND .Addr AND .ShipDate AND .Billing AND .PayMethod
   End With
End Sub

If there was a place in the code that I could click without triggering a LostFocus, I would call a validation routine when I click it, e.g.:

Sub mnuDoSomething_Click ()

   ValidateData
   CheckValidData
   ' ... other code here ...

End Sub

ValidateData would either call all of the appropriate _Lostfocus events (if all they did was check for the existance of the data), or check the data validity for each field itself.

Does this seem workable for your situation?

Author

Commented:
Hi bhess1,

I'm not exactly a programmer, to say the least, so I don't follow the UDT abreviation... User Defined Type right?  Where to I put this code in terms of form, module etc being a public definition?  I think what you are suggesting can be modified enough to give me what I want...  Could you spend a little time explaining it slow time to me?  maybe which forms should trigger what...

Thanx,
-siegi
Brendt HessSenior DBA
CERTIFIED EXPERT

Commented:
Sure can...

The UDT should be defined in a module (not a form).  You then need to DIM a variable for of the UDT type, probably in the same module, as Public, e.g.:

Public DataEntered as DataEnteredFlags

You could place the validation routine and the button activation routine in either your parent MDI form, or in a module.  

The calls to the validation routine would be placed wherever you are having problems with not getting the LostFocus / GotFocus.

The calls to the button activation routine (CheckValidData in my example) would be placed (a) after you set all of the flags to false, (b) wherever you enter the data, and (c) wherever you call the validation routine.

The validation routine might look something like this:

Sub ValidateData()

   With DataEntered
      .Name = (MDIChild1.txtCustomerName > "")
      .Addr = (MDIChild1.txtCustomerAddr > "")
      .ShipDate = IsDate(MDIChild2.txtShipDate)
      ' other checks
   End With
End If

Does this make it clearer?

Author

Commented:
Hi bhess1,
sorry for the long delay.. The email post/reply wasn't working so I didn't get a note stating a new response was posted.  Anyways I'll be looking at it today and tomorrow... I'll let you know.
Thanx,
-siegi
CERTIFIED EXPERT
Author of the Year 2009

Commented:
Hi siegi,
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 bhess1's comment(s) as an answer.

siegi, 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

Author

Commented:
Hey Dan,

...I didn't forget about the question, just haven't received any answers that I was looking for. As I mentioned above a while ago, bhess1 is closer than anyone else that left a response so I can accept his as an answer. However this may not help expert exchange's answer to question truety ratio when you pay for an already accepted answer to a question that appears to be similar to the one being asked. Quite a few times I have found answers for questions such as this that were not acceptable nor did they help me... However in sake for house keeping I'll accept this answer.

Cheers,
~Mike
Per recommendation, force-accepted.

Netminder
CS Moderator

Explore More ContentExplore courses, solutions, and other research materials related to this topic.