Solved

C# WinForm Access a Form element from another form?

Posted on 2011-03-09
5
839 Views
Last Modified: 2013-12-17
Hi all,

i want to access a panel inside a form from another form

so i have
Form1.Panel

in form 2
Form1.Panel.Controls.Add(mystuff)

how do i do it

From1.Panel says object reference required for non static...

Thanks for any help

0
Comment
Question by:awilderbeast
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35084009
Hi,

Try to change the MODIFIERS property of the Panel to PUBLIC
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 250 total points
ID: 35084071
You also need to pass a reference of the Form1 instance to Form2.  You can do that on the constructor of Form2.  Something like:

    public partial class Form2 : Form
    {
        private Form1 _form1;

        public Form2(Form1 form1)
        {
            InitializeComponent();

            _form1 = form1;
        }
    }

Open in new window


Then in Form2:
_form1.Panel.Controls.Add(mystuff)

To instantiate Form2 (assuming the code is in Form1):
new Form2(this)
0
 
LVL 4

Expert Comment

by:parnasso
ID: 35084098
The compiler is telling you that  an instance of Form1 is needed to access the properties Panel and Controls:

Form1 form = new Form1();

Now you can add your stuff to the created instance:

form.Panel.Controls.Add(mystuff);




0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 250 total points
ID: 35084125
You have a couple methods available to you.  One is to modify the constructor of Form2 so that it accepts one parameter of type Form1. Store this parameter as a private variable in Form2, _form1 for example.  When Form2 needs to set something on Form1 it can do so using _form1.Panel.Controls.Add(...).  The Panel on Form1 also needs to be publicly accessible, as stated above (in the Properties window for the panel on Form1, change Modifiers to "public").

Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void ShowForm2Button_Click(object sender, EventArgs e)
		{
			new Form2(this).Show(); // Pass the constructor of Form2 a reference to this "Form1" instance
		}
	}
}

Open in new window


Form2.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
	public partial class Form2 : Form
	{
		private Form1 _form1; // Private variable to hold reference to a "Form1"

		public Form2(Form1 form1)
		{
			InitializeComponent();

			_form1 = form1;
		}

		private void AddStuffToForm1Button_Click(object sender, EventArgs e)
		{
			_form1.panel1.Controls.Add(...);
		}
	}
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:awilderbeast
ID: 35084308
Thanks guys
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question