• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

How do I change an active control on another form from a MDI parent?

I have a MDI parent and one MDI child.  The MDI parent has a tool bar. I want to push one of the toolbar buttons that will have code to click another button on the child.  So far I have only been able to get it to work if the button is the active control on the child, in this case if the tab stop is 0.  Writing the code in visual basic.

Thanks
0
GCCal
Asked:
GCCal
  • 5
  • 5
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is a simple example...

The MDI Child form with just a button on it:

    Public Class MdiChild

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MessageBox.Show("Hello from MdiChild!")
        End Sub

    End Class

The MDI Parent form with a button on it:
    Public Class MdiParent

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim frm As Form = Me.ActiveMdiChild
            If Not IsNothing(frm) AndAlso TypeOf (frm) Is MdiChild Then
                Dim mc As MdiChild = DirectCast(frm, MdiChild)
                mc.Button1.PerformClick()
            End If
        End Sub

    End Class
0
 
GCCalAuthor Commented:
Tried the code, but getting an error that mdiChild is not defined.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I'm not suprised that your getting an error...  =\

"MdiChild" is the name of MY form that is being used as an MdiChild form.

YOU need to REPLACE it with whatever the Name of YOUR MdiChild is:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim frm As Form = Me.ActiveMdiChild
            If Not IsNothing(frm) AndAlso TypeOf (frm) Is {{{InsertYour_OWN_MdiChildTypeHere}}} Then
                Dim mc As {{{InsertYour_OWN_MdiChildTypeHere}}} = DirectCast(frm, {{{InsertYour_OWN_MdiChildTypeHere}}})
                mc.Button1.PerformClick()
            End If
        End Sub

Notice I used a really long, exaggerated placeholder above to illustrate where you need to make changes:

    {{{InsertYour_OWN_MdiChildTypeHere}}}
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
GCCalAuthor Commented:
Okay, I set the code like this:
        Dim frm As Form = Me.ActiveMdiChild
        If Not IsNothing(frm) AndAlso TypeOf (frm) Is frmCVR Then
            Dim mc As frmCVR = DirectCast(frm, frmCVR)
            mc.Button10.PerformClick()
        End If

but the button10 still does not activate.  Does it look like I did it right?
Thanks,
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That looks good from here...

The code in Button10 isn't running?
0
 
GCCalAuthor Commented:
Yes, the click event does not occur.  However, if I click the button with the mouse the click event occurs and the code executes as it should.  So what I have is the MDI form showing with the  child displayed.  I click the button on the MDI tool strip and no reaction.  Then I click the button on the child and it excutes.

Thanks,
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hmmm....something must be different about your setup as it worked fine for me in a simple project.

Try creating a new project with ONLY the MdiParent form and ONE MdiChild with a simple MessageBox in the Button event.
0
 
GCCalAuthor Commented:
Yes, it worked in a new project so I have something incorrect in my application. I will work at it.

Thanks for your help Idle Mind.

GCCal
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Glad you were able to make the concept work for you.

Not sure where to guide you on finding the difference in your existing application though.  =\

My only thought is that somehow you don't have an "active" MdiChild and this line is returning Nothing:

    Dim frm As Form = Me.ActiveMdiChild
0
 
GCCalAuthor Commented:
First time I used your service.  Great idea.  
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now