Are Form1 and Form2 classes or object instances?
If they are classes then you must create instances first.
// Going from memory syntax may be a little off.
public class Form1 : Form
{
// Declare buttons etc
public Form1()
{
InitializeComponent();
Form2 form = new Form2(this);
form.Show();
}
public void AddText(string text)
{
// Do something useful with text here
}
[STAThread]
static void Main( string[] args )
{
Application.Run(new Form1());
}
}
public class Form2 : Form
{
private _form1 callingForm;
//Declare buttons etc
public Form2(Form1 caller)
{
callingForm = caller;
InitializeComponent()
}
// Handlker for your button click event
protected Button_Click(object sender, EventArgs e)
{
callingForm.AddText("Add this text");
}
}
"rgreen3" <an*******@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
Form1 is a PUBLIC class with a button, a label, and a PUBLIC method called
AddText(). Form2 is a PUBLIC class with a button.
When Form1.button is pressed it opens Form2 using the Show method.
The button on Form2 should execute the AddText() method on Form1 which
simply adds some text to the label. I would have thought that I could just
write something like this in the Form2 Button_Click event handler:
Form1.AddText();
but it doesn't work. I get the following error message if I try to
compile:
An object reference is required for the nonstatic field, method, or
property 'WindowsApplication62.Form1.AddText()'
I also tried passing Form1 as part of the constructor for Form2 but it
still would not work.
Can anyone shed some light on this?