Solved

C# WinForm Access a Form element from another form?

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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 aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

815 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

10 Experts available now in Live!

Get 1:1 Help Now