Accessing one form’s controls from another form
September 16th, 2007
Here's the situation:
In a .NET appication I have a form containing several controls (lets call this MainForm). I want to allow the user to change many of the properties of this form, such as the background colour, the labels, the font, etc. To do this I have a separate form (PropertiesForm). The question is, how do I access the properties of the MainForm controls from within PropertiesForm.
You might think that it was a simple case of making the MainForm class and/or the control objects declared within MainForm public rather than private. But this isn't enough. Instead you need to declare an internal (or public) variable whose type is the name of the class you want to access (in my case MainForm) and then when you open the new form from MainForm set that variable to the MainForm form. That sounds complicated, but it's easier to show it than to explain it in writing.
Here's part of the PropertiesForm code from a file called Form2.cs:
namespace MyCsharpProgram
{
public partial class PropertiesForm : Form
{
//Declare a variable for the main form:
internal MainForm ParentForm;
...
private void buttonUpdateForm_Click(object sender, EventArgs e)
{
//Change the text of the exit button:
ParentForm.buttonExit.Text = sExitButtonText;
...
}
}
}
In the above code, clicking the "buttonUpdateForm" button on the PropertiesForm form changes the text of a button called buttonExit on the parent form.
Here's part of the MainForm code from a file called Form1.cs
namespace MyCsharpProgram
{
public partial class MainForm : Form
{
...
private void linkLabelProperties_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PropertiesForm PropertiesForm = new PropertiesForm();
PropertiesForm.ParentForm = this;
PropertiesForm.ShowDialog();
}
...
}
}
Here, in MainForm, I have an event handler for a link label called linkLabelProperties. When this link label is clicked we create a new instance of PropertiesForm and we set the object called ParentForm to this form (i.e. MainForm). We then display the form.
What we're doing here is passing a reference to the MainForm form into the PropertiesForm form, so that you can access MainForm objects from PropertiesForm.
The Form1.Designer.cs file contains the definition of the buttonExit button, which is declared as internal, rather than private, so that we can access it from another class:
namespace VocabBuilder
{
public partial class VocabBuilderForm
{
...
internal System.Windows.Forms.Button buttonExit;
...
}
}
I should add that if you're writing serious code this is not the correct way to do this. How you should do it is to use delegates. This makes the classes and methods much more independent and makes it much easier to maintain them in a large body of code, and to reuse classes in other programs.
Potentially similar posts
- Convert escaped Unicode to HTML entities – January 2012
- My first (useful) Ruby program – June 2011
- Adding your choice of text editor to Flare’s Open With menu – December 2010
- Use the existence of a file on the server to determine Javascript behaviour in the browser – November 2010
- ITauthor podcast #34 – Testing testing 123 – May 2010