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
Solved

C# WinForm Access a Form element from another form?

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

792 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