bpayne111 Posted April 1, 2003 Posted April 1, 2003 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. Quote i'm not lazy i'm just resting before i get tired.
*Gurus* divil Posted April 1, 2003 *Gurus* Posted April 1, 2003 I think the parent form has an MdiChildActivate event. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
bpayne111 Posted April 1, 2003 Author Posted April 1, 2003 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. Quote i'm not lazy i'm just resting before i get tired.
bpayne111 Posted April 1, 2003 Author Posted April 1, 2003 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? '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 Quote i'm not lazy i'm just resting before i get tired.
*Experts* Nerseus Posted April 1, 2003 *Experts* Posted April 1, 2003 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
bpayne111 Posted April 2, 2003 Author Posted April 2, 2003 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 Quote i'm not lazy i'm just resting before i get tired.
*Gurus* divil Posted April 2, 2003 *Gurus* Posted April 2, 2003 No, not correct. The event you use to tell when Mdi Children are switched between is the MdiChildActivate event on the parent form. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
bpayne111 Posted April 2, 2003 Author Posted April 2, 2003 how can you determine which child is activated? Quote i'm not lazy i'm just resting before i get tired.
*Experts* Nerseus Posted April 2, 2003 *Experts* Posted April 2, 2003 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. private void Form1_MdiChildActivate(object sender, System.EventArgs e) { Form2 f = (Form2)this.ActiveMdiChild; MessageBox.Show(f.Handle.ToInt32().ToString() + ": " + f.Text); } -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
bpayne111 Posted April 2, 2003 Author Posted April 2, 2003 thanks alot i figured it out now. that C# code kinda hurt my eyes but i made it through thanks Quote i'm not lazy i'm just resting before i get tired.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.