473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Executing Methods on a Different Form

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

Nov 15 '05 #1
14 2699
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" <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?

Nov 15 '05 #2
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" <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?

Nov 15 '05 #3
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?
--
B\rgds
100
Nov 15 '05 #4
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?
--
B\rgds
100
Nov 15 '05 #5
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.
Nov 15 '05 #6
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.
Nov 15 '05 #7
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

Nov 15 '05 #8
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

Nov 15 '05 #9
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?

Nov 15 '05 #10
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?

Nov 15 '05 #11
Hi rgreen3,

"rgreen3" <an*******@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
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
Nov 15 '05 #12
Hi rgreen3,

"rgreen3" <an*******@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
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
Nov 15 '05 #13
That was it

Thanks guys for all your help.
Nov 15 '05 #14
That was it

Thanks guys for all your help.
Nov 15 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
18
by: Brad Pears | last post by:
Can someone give me some sample code on how one would go about executing a command line "command" from within an ASP form? We need to run an application called GnuPG which allows us to encrypt an...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
4
by: excelleinc.com | last post by:
Hello, I'm trying to put sub that's shared between all forms in my app in module so when I make change I just change it one time. This sub should execute and then invoke sub defaults() that's...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
2
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
I am executing an AJAX page method that is a long running task. After starting the first method, I execute a second page method to retrieve the status of the task. It works fine in an empty web...
5
by: Gerardo Herzig | last post by:
Hi all. Im in this situation: I want to perform several kind of (validating) methods to a given value. Lets say i have a class named Number, and the following methods: is_really_a_number(),...
7
by: robin1983 | last post by:
Hi, good morning everyone, i have a file called attendence.php The problem is that some part of code is executing properly and half of the code is not and i dont get any warning or error message. For...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.