Solved

C# WinForm Access a Form element from another form?

Posted on 2011-03-09
5
819 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
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now