We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

Medium Priority
352 Views
Last Modified: 2013-11-26
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
Comment
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Tried the code, but getting an error that mdiChild is not defined.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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}}}

Author

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,
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
That looks good from here...

The code in Button10 isn't running?

Author

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,
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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.

Author

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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Author

Commented:
First time I used your service.  Great idea.  
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.