Activated event doesn't fire????

bpayne111

Junior Contributor
Joined
Feb 28, 2003
Messages
325
Location
BFE
To keep it simple...
I have a form in an MDI app and i'd like to show a message box when it is activated. The activate event never fires when i load the child form. Is this because it's a child form? I think i've almost got it figured out but i'd like to hear any comments on the subject.
 
yes i was playing with that as i wrote the post... why would they do this? i don't get it. It seems impossible to get the 'true' activate event to fire no matter what i do.
 
could it be that there is just a bug in my version of vs.net? me and my teacher are playing with this and well basically you can't predict when the activate event is gonna fire. Sometimes it works sometimes it doesn't. I have version 1.0.3705 of the framework and i can't upgrade. Could this be my issue?

Visual Basic:
'frmMain
Dim frmTest as form 'modular variable

 Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick

        Select Case ToolBar1.Buttons.IndexOf(e.Button)
            Case 0
                'show frmKandles
                If FormLoaded("frmKandles") Then
                    frmTest.Activate()
                Else
                    frmTest = New frmKandles()
                    frmTest.MdiParent = Me
                    frmTest.Show()
                End If
            Case 1
                'show frmSummary
                If FormLoaded("frmSummary") Then
                    frmTest.Activate()
                Else
                    frmTest = New frmSummary()
                    frmTest.MdiParent = Me
                    frmTest.Show()
                End If
            Case 2
                'show frmAbout
                If FormLoaded("frmAbout") Then
                    frmTest.Activate()
                Else
                    frmTest = New frmAbout()
                    frmTest.MdiParent = Me
                    frmTest.Show()
                End If
        End Select
    End Sub

    Public Function FormLoaded(ByVal strName As String) As Boolean
        'check to see if form is loaded

        For Each frmTest In Me.MdiChildren
            If frmTest.Name = strName Then
                Return True
            End If
        Next
    End Function
  
 Private Sub frmMain_MdiChildActivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MdiChildActivate
        'work to be done
        If FormLoaded("frmSummary") Then
            frmTest.Activate()
        End If
    End Sub


'code in child form frmSummary
 Private Sub frmSummary_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        MessageBox.Show("hey 2")
    End Sub
 
I have the same version of the framework (build 3705) and I have no trouble with the Activated event firing on the child form.

Here's what I did:
1. Create new C# winform project
2. Change Form1's IsMdiContainer property to true
3. Added a second form (Form2) and added an Activated event with a MessageBox.Show
3. Added a main menu and click event to Form1
4. In the click event I create a new instance of Form1, set it's MdiParent property to Form1 (I use "this"), and show the new form2.

I see the messagebox, no worries.
Maybe you could post a zip of your project and I could test it here, on my machine?

-nerseus
 
if you still have that project saved... try using 2 different child forms. And try getting the Activate event to fire anytime AFTER the first activate for that child form.
ie. Load both forms as children. Then click back and forth between them and see if it will fire then.
The activate event should fire each time the form 'recieves focus' correct?

I'll send the code in my next post if needed
Thanks
 
No, not correct. The event you use to tell when Mdi Children are switched between is the MdiChildActivate event on the parent form.
 
Ah, didn't test my code far enough - only first the Activated event on the child form the first time the first instance is shown. Never fires again.

You can use the ActiveMdiChild property of the MDIParent form to get the active child form. Since you're accessing this in the MdiChildActivate event, it should be the one that's receiving focus. I tested with the following code and it worked like a charm. Of course, I only show Form2 as a child - if you have different child forms, you can't use the direct casting as I've done here.

C#:
private void Form1_MdiChildActivate(object sender, System.EventArgs e)
{
	Form2 f = (Form2)this.ActiveMdiChild;
	MessageBox.Show(f.Handle.ToInt32().ToString() + ": " + f.Text);
}

-Nerseus
 
Back
Top