Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# WinForm Access a Form element from another form?

Posted on 2011-03-09
5
Medium Priority
?
847 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 1000 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 1000 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

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

719 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