Executing Methods on a Different Form

G

Guest

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
 
J

joseph.inglis

hint. define the text box object as static in Form1 and refernce it
directly.

in Form1
private internal static TextBox box1 = new TextBox();

and from Form2
Form1.box1.Text = "text";

Im probably gonna get shouted at by the community for suggesting this ;-)

Tam

rgreen3 said:
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()'
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi rgreen3,
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()'
Yes, that's right. AddText() is not static and it couldn't be. so you need
reference to the instance of class Form1
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?

This is the way to go. What do you pass? You have to pass a reference
(variable). What the compiler says?
 
G

Guest

Thanks but that's not an option. This is a demo I set up to find out why this wouldn't work. The actual problem I am trying to solve is more involved than this.
 
G

Guest

The constructor for Form2 looks like this
public Form2(ref Form Form1

The call to the constructor in form1 looks like this
Form2 fm2=new Form2(ref Form1)

This is the error
'WindowsApplication62.Form1' denotes a 'class' where a 'variable' was expecte
 
B

Brian W

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 said:
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()'
 
D

Daniel Pratt

Hi rgreen3,

rgreen3 said:
The constructor for Form2 looks like this:
public Form2(ref Form Form1)

The call to the constructor in form1 looks like this:
Form2 fm2=new Form2(ref Form1);

This is the error:
'WindowsApplication62.Form1' denotes a 'class' where a 'variable' was
expected

You almost certainly don't want to use "ref". "ref" implies that you
would want to be able to change the external reference that was passed in to
reference a completely different object (or null). This code is within class
Form1, yes? If so, you want to pass "this" (the reference to the calling
form) to the constructor of Form2:

Form2 fm2 = new Form2(this);

Regards,
Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top