Winforms Controls

Hello,
I have a mdi parent with a child form.
On the child form I have a panel and on the panel I have button1 and button2
How can I access the buttons elements from the parent mdi in vb?

Thanks
LVL 1
H-SCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Are you talking about something like:

Form1.vb -
Public Class Form1
	Private child As Form2 = Nothing

	Private Sub OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChild.Click
		If child Is Nothing Then
			child = New Form2()
			child.Show()
			child.Button1.PerformClick()
			child.Button2.PerformClick()
		End If
	End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.btnChild = New System.Windows.Forms.Button()
		Me.SuspendLayout()
		'
		'btnChild
		'
		Me.btnChild.Location = New System.Drawing.Point(197, 227)
		Me.btnChild.Name = "btnChild"
		Me.btnChild.Size = New System.Drawing.Size(75, 23)
		Me.btnChild.TabIndex = 1
		Me.btnChild.Text = "Open Child"
		Me.btnChild.UseVisualStyleBackColor = True
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 262)
		Me.Controls.Add(Me.btnChild)
		Me.IsMdiContainer = True
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents btnChild As System.Windows.Forms.Button

End Class

Open in new window

Form2.vb -
Public Class Form2
	Private Sub OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click, Button1.Click
		RichTextBox1.AppendText(String.Format("{0} was clicked.{1}", DirectCast(sender, Button).Name, Environment.NewLine))
	End Sub
End Class

Open in new window

Form2.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form2
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.Panel1 = New System.Windows.Forms.Panel()
		Me.Panel2 = New System.Windows.Forms.Panel()
		Me.RichTextBox1 = New System.Windows.Forms.RichTextBox()
		Me.Button1 = New System.Windows.Forms.Button()
		Me.Button2 = New System.Windows.Forms.Button()
		Me.Panel1.SuspendLayout()
		Me.Panel2.SuspendLayout()
		Me.SuspendLayout()
		'
		'Panel1
		'
		Me.Panel1.Controls.Add(Me.Button2)
		Me.Panel1.Controls.Add(Me.Button1)
		Me.Panel1.Dock = System.Windows.Forms.DockStyle.Top
		Me.Panel1.Location = New System.Drawing.Point(0, 0)
		Me.Panel1.Name = "Panel1"
		Me.Panel1.Size = New System.Drawing.Size(284, 156)
		Me.Panel1.TabIndex = 0
		'
		'Panel2
		'
		Me.Panel2.Controls.Add(Me.RichTextBox1)
		Me.Panel2.Dock = System.Windows.Forms.DockStyle.Bottom
		Me.Panel2.Location = New System.Drawing.Point(0, 162)
		Me.Panel2.Name = "Panel2"
		Me.Panel2.Size = New System.Drawing.Size(284, 100)
		Me.Panel2.TabIndex = 1
		'
		'RichTextBox1
		'
		Me.RichTextBox1.Dock = System.Windows.Forms.DockStyle.Fill
		Me.RichTextBox1.Location = New System.Drawing.Point(0, 0)
		Me.RichTextBox1.Name = "RichTextBox1"
		Me.RichTextBox1.Size = New System.Drawing.Size(284, 100)
		Me.RichTextBox1.TabIndex = 2
		Me.RichTextBox1.Text = ""
		'
		'Button1
		'
		Me.Button1.Location = New System.Drawing.Point(12, 130)
		Me.Button1.Name = "Button1"
		Me.Button1.Size = New System.Drawing.Size(75, 23)
		Me.Button1.TabIndex = 0
		Me.Button1.Text = "Button1"
		Me.Button1.UseVisualStyleBackColor = True
		'
		'Button2
		'
		Me.Button2.Location = New System.Drawing.Point(197, 130)
		Me.Button2.Name = "Button2"
		Me.Button2.Size = New System.Drawing.Size(75, 23)
		Me.Button2.TabIndex = 1
		Me.Button2.Text = "Button2"
		Me.Button2.UseVisualStyleBackColor = True
		'
		'Form2
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 262)
		Me.Controls.Add(Me.Panel2)
		Me.Controls.Add(Me.Panel1)
		Me.Name = "Form2"
		Me.Text = "Form2"
		Me.Panel1.ResumeLayout(False)
		Me.Panel2.ResumeLayout(False)
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents Panel1 As System.Windows.Forms.Panel
	Friend WithEvents Panel2 As System.Windows.Forms.Panel
	Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
	Friend WithEvents Button2 As System.Windows.Forms.Button
	Friend WithEvents Button1 As System.Windows.Forms.Button
End Class

Open in new window

Which produces the following output -Initial loadClicking the Open Child button in Form1 opens Form2 and clicks the buttons on Form2.-saige-
Fernando SotoRetiredCommented:
Hi H-SC;

The MDI parent Form has a property called MdiChildren which is an array of the MdiChildren forms. You can iterate through and get a reference to the child you want. Once you have that reference to the child then you can access all the public properties.
Jacques Bourgeois (James Burger)PresidentCommented:
What do you want to do when you specify "access" the buttons. Reading and settings properties, or doing something else?

If you mean working with their properties, then you do it with something similar to the following:

            Dim yourChild As New Form1
            yourChild.MdiParent = Me
            yourChild.Show()
            '... and when you are ready ...
            yourChild.Button1.Text = "Click here!"
            yourChild.Button2.Text = "No, click me!"

If the creation of the form and the code that needs to work with the buttons are not in the same method, simply declare the yourChild variable at the Form level so that it is accessible everywhere.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

H-SCAuthor Commented:
it_saige,
It is more like what James is explaining.
H-SCAuthor Commented:
Fernando,
Do you have an example of how that would work?
H-SCAuthor Commented:
James,
I have your solution working however, I already have the child form showing on parent load and when I use your example, it opens another form over the existing child within the container.  How can I keep the original open and access the visibility properties of any given button on the child?
Jacques Bourgeois (James Burger)PresidentCommented:
Declare your form variable at the class level, in the form's declaration section. The variable is then available everywhere in the form. You can then show the form in the Load event, and the variable will be available anywhere you need it.
H-SCAuthor Commented:
James,
Many thanks, for some reason I still cant get it to work.  It keeps opening a new child form on each call
Jacques Bourgeois (James Burger)PresidentCommented:
You probably declare a new variable each time.

As long as you have declared your first instance variable in the Declarations section of the form and displayed it in Form_Load, the following is sufficient:

        yourChild.Button1.Text = "Click here!"
        yourChild.Button2.Text = "No, click me!"
H-SCAuthor Commented:
James,
That worked perfect!  Many Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.