在不同的表格上执行方法 [英] Executing Methods on a Different Form

查看:52
本文介绍了在不同的表格上执行方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Form1是一个PUBLIC类,带有一个按钮,一个标签和一个名为AddText的PUBLIC方法()

Form2是一个带按钮的PUBLIC类


当按下Form1.button时,它使用Show方法打开Form2

Form2上的按钮应该在Form1上执行AddText()方法,只需在标签上添加一些文本。我原本以为我可以在Form2 Button_Click事件处理程序中写这样的东西


Form1.AddText()


但它没有不行。如果我尝试编译,我会收到以下错误消息


非静态字段,方法或属性''WindowsApplication62.Form1.AddText()需要对象引用


我也尝试将Form1作为Form2的构造函数的一部分传递,但它仍然无效。


任何人都可以对此有所了解

解决方案

提示。在Form1中将文本框对象定义为静态并直接引用它



Form1中的


私有内部静态TextBox box1 =新的TextBox();


和来自Form2

Form1.box1.Text =" text";


我可能会被社区大声吼叫建议这个;-)


Tam


" rgreen3" <一个******* @ discussions.microsoft.com>在留言中写道

新闻:D0 ********************************** @ microsof t.com ...

Form1是一个PUBLIC类,带有一个按钮,一个标签和一个名为
AddText()的PUBLIC方法。 Form2是一个带按钮的PUBLIC类。

当按下Form1.button时,它使用Show方法打开Form2。
Form2上的按钮应该在Form1上执行AddText()方法,
只是在标签上添加了一些文字。我原本以为我可以在Form2 Button_Click事件处理程序中写这样的东西:
Form1.AddText();

但它没有'''工作。如果我尝试
编译,我会收到以下错误消息:
非静态字段,方法或
属性需要对象引用''WindowsApplication62.Form1.AddText()''<我还尝试将Form1作为Form2的构造函数的一部分传递,但是
仍然无效。
任何人都可以对此有所了解吗?



提示。在Form1中将文本框对象定义为静态并直接引用它



Form1中的


私有内部静态TextBox box1 =新的TextBox();


和来自Form2

Form1.box1.Text =" text";


我可能会被社区大声吼叫建议这个;-)


Tam


" rgreen3" <一个******* @ discussions.microsoft.com>在留言中写道

新闻:D0 ********************************** @ microsof t.com ...

Form1是一个PUBLIC类,带有一个按钮,一个标签和一个名为
AddText()的PUBLIC方法。 Form2是一个带按钮的PUBLIC类。

当按下Form1.button时,它使用Show方法打开Form2。
Form2上的按钮应该在Form1上执行AddText()方法,
只是在标签上添加了一些文字。我原本以为我可以在Form2 Button_Click事件处理程序中写这样的东西:
Form1.AddText();

但它没有'''工作。如果我尝试
编译,我会收到以下错误消息:
非静态字段,方法或
属性需要对象引用''WindowsApplication62.Form1.AddText()''<我还尝试将Form1作为Form2的构造函数的一部分传递,但是
仍然无效。
任何人都可以对此有所了解吗?



你好rgreen3,

Form1.AddText();

但它不起作用。如果我尝试
编译,我会收到以下错误消息:
非静态字段,方法或
属性需要对象引用''WindowsApplication62.Form1.AddText()''

是的,那是对的。 AddText()不是静态的,它不可能。所以你需要

引用类Form1的实例

我也尝试将Form1作为Form2的构造函数的一部分传递,但是
仍然无法工作。有谁可以对此有所了解?




这是要走的路。你通过什么?你必须传递一个参考

(变量)。编译器说的是什么?

-

B \ rgds



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

解决方案

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?



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?



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


这篇关于在不同的表格上执行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆