如何在Windows Applicationmode中传递表单之间的信息,类 [英] How to pass information, classes between forms in Windows Applicationmode

查看:65
本文介绍了如何在Windows Applicationmode中传递表单之间的信息,类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



关键字:范围解析,在父和子之间传递类

表,参数构造方法,普通构造函数,默认

构造函数,前向引用,在表单之间共享类。


这是我发现自己做的一个新手错误(作为一个新手),并且即使是一个主程序员,也是
,这个论坛的大师,Jon Skeet,

错过了! (他知道我很有信心,但我认为这不是我的问题; LOL,我正在给他打气)


如果你想在两个由

类组成的表格之间传递信息,无论它们是父/子,模态还是无模式,

对话框或你必须使用的非对话非正常/非默认或

参数化或参数构造函数,而不是默认值(无参数)

普通构造函数。下面的说明。


假设您在表单上有一个按钮,您单击以显示

另一个表单,该按钮有一个事件处理程序名为

" clikMeButton2BringUpANew_Child_Form_to_The_MainFo rm_Click" ;.


让我们说在设计师模式中你已经建立了一个不错的新人(孩子或者/>
秒或依赖或非主要形式称为Form2形式(我认为你可以

也可以通过编程''构建''一个新表单,但我喜欢使用Designer

模式,拖放并让向导形象如何初始化

表格,但我离题了。


假设你有一个名为Class1的类,你在你的声明

第一个表单,在实例化该表单期间,有一个实例

" myClass1",现在你想要同一个类实例化

传递给Form2。怎么做?如果你使用了错误的

构造函数,它就不会被新形式看到。因此你必须这样做:
这个:


private void

clikMeButton2BringUpANew_Child_Form_to_The_MainFor m_Click(object

sender, EventArgs e)

{

// Form2 frm2 = new Form2(); //不要这样做 - 它不会是
工作!你不能在frm2中看到myClass1

Form2 frm2 = new Form2(myClass1); //这样可以工作;

myClass1将在Form2中看到

//当然,我们假设你在Form2中有一个参数构造函数

接受class1作为参数,即,你有一个带有

的构造函数,这个签名在Form2中(Class1 C1){}; //见下文(#1)


frm2.Show(); //显示表格等

}


就是这样。完成。你也可以通过一个以上的课程,

并带有正确的签名。


(#1)为了使这一点更加清晰,Form2必须除了普通(默认)非参数

构造函数之外,还有一些构造函数,除了以下几行:


public Form2( Class1 passClass1_from_Form1)

{

myClass2_that_exists_in_Form2 = passClass1_from_Form1; //

名字当然是随意的


InitializeComponent();


//其他东西如果你在这里想要

}


完成。


另外还有一件事,它与之无关以上:

您需要在表格1中小心放置class1;它必须是类似于C ++的b $ b,并且在Form1构建之前作为前向引用放置,并按照以下方式构建:


公共部分类Form1:表格

{

Class1 myClass1; //注意它的位置!在Form1的

默认构造函数之外

//这在C ++中被称为''前向引用'',你需要做

这对于C#以及

public Form1()

{

InitializeComponent(); //强制所有表格,东西

向导填充而且我不会傻到


myClass1 = new Class1();

//或者,如果你的Class1接受参数int i,string s,myclass1 = new

Class(10," hi");


//这里的其他东西,比如,this.AutoValidate =

AutoValidate.EnableAllowFocusChange; //等


}


///其他东西


}


重点是:如果Class1不是

''嵌套''在Form1 {}中,你需要一个前向引用,也就是说,如果Class1属于一个单独的

翻译单元或模块,即使Class1与

Form1在同一名称空间中。


RL


Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms.

Here is a newbie mistake that I found myself doing (as a newbie), and
that even a master programmer, the guru of this forum, Jon Skeet,
missed! (He knows this I''m sure, but just didn''t think this was my
problem; LOL, I am needling him)

If you want to pass information between two forms comprised of
classes, whether or not they are parent/child, modal or modeless,
dialog or non-dialog you have to use the non-normal/ non-default or
parametricized or parameter constructor, not a default (no parameter)
normal constructor. Explanation below.

Let''s say you have a button on your form that you click to bring up
another form, and the button has an Events handler called
"clikMeButton2BringUpANew_Child_Form_to_The_MainFo rm_Click".

Let''s say in Designer mode you''ve already built a nice new (child or
second or dependent or non-main) form called "Form2" (I think you can
also programmically ''build'' a new form, but I like using the Designer
mode, drag-and-drop and let the Wizard figure out how to initialize
the form, but I digress).

Let''s say you have a class, called Class1, that you declared in your
first form, during instantiation of that form, having an instance
"myClass1", and now you want that same class instantiation to be
passed to the Form2. How to do that? If you use the wrong
constructor it won''t be ''seen'' by the new form. Hence you must do
this:

private void
clikMeButton2BringUpANew_Child_Form_to_The_MainFor m_Click(object
sender, EventArgs e)
{
// Form2 frm2 = new Form2(); //Do not do this--it won''t
work! You cannot see myClass1 in frm2

Form2 frm2 = new Form2(myClass1); // this will work;
myClass1 will be seen in Form2
// of course, we assume you do have a parameter constructor in Form2
that accepts class1 as a parameter, ie., you have a constructor with
this signature in Form2 (Class1 C1) {};//see further below at (#1)

frm2.Show(); //shows the form, etc
}

That''s it. Done. You can also pass more than one class of course,
with the proper signature.

(#1) And just to make the point clearer, Form2 must have as a
constructor something besides the normal (default) non parameter
constructor, along these lines:

public Form2(Class1 passClass1_from_Form1)
{
myClass2_that_exists_in_Form2 = passClass1_from_Form1; //
names are arbitrary of course

InitializeComponent();

// other stuff here if you want
}

Done.

One more thing as an aside, and it has nothing to do with the above:
you need, in Form 1, to be careful where you put class1; it has to be
sort of like C++, and placed as a ''forward reference'' before Form1 is
constructed, along these lines:

public partial class Form1 : Form
{
Class1 myClass1; //Note where this is located! outside the
default constructor for Form1
// this is called a ''forward reference'' in C++, and you need to do
this for C# as well
public Form1()
{
InitializeComponent(); //mandatory for all forms, stuff
that the Wizard fills and I don''t fool with

myClass1 = new Class1();
//or, if your Class1 takes parameters int i, string s, myclass1=new
Class (10, "hi");

//other stuff here, like, for example, this.AutoValidate =
AutoValidate.EnableAllowFocusChange; //etc

}

/// other stuff here

}

The point being: you need a forward reference if the Class1 is not
''nested'' in the Form1 {}, that is, if Class1 is part of a separate
translation unit or module, even if Class1 is in the same namespace as
Form1.

RL

推荐答案

raylopez99< ra ******** @ yahoo.comwrote:
raylopez99 <ra********@yahoo.comwrote:

关键字:范围分辨率,父和子之间传递类

表格,参数构造函数方法,普通构造函数,默认

构造函数,前向引用,表单之间共享类。 />

这是我发现自己做的一个新手错误(作为新手),以及

即使是一个主程序员,这个论坛的大师,Jon Skeet,

错过了! (他知道这一点我很有信心,但我认为这不是我的问题; LOL,我正在给他打气)
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms.

Here is a newbie mistake that I found myself doing (as a newbie), and
that even a master programmer, the guru of this forum, Jon Skeet,
missed! (He knows this I''m sure, but just didn''t think this was my
problem; LOL, I am needling him)



我仍​​然认为这不是你的问题。你仍然感到困惑 - 并且

你从访问变量中改变了问题的更多内容。

信息传递。

I still don''t think it''s your problem. You''re still confused - and
what''s more you''ve changed the problem from "accessing a variable" to
"information passing".


如果你想在两个表格之间传递信息

类,无论它们是父/子,模态还是无模式,

对话框或非对话框你必须使用非正常/非默认或

参数化或参数构造函数,而不是默认值(无参数)

普通构造函数。说明如下。
If you want to pass information between two forms comprised of
classes, whether or not they are parent/child, modal or modeless,
dialog or non-dialog you have to use the non-normal/ non-default or
parametricized or parameter constructor, not a default (no parameter)
normal constructor. Explanation below.



当然,如果你想将信息从一个上下文传递给一个新的
对象,那么使用参数化构造函数就可以了。这与

无关,但是能够访问另一种类型的变量

虽然。


只是传递一个参数并不意味着Form2神奇地能够获得
,请参阅Form1.myClass1。 Form2的构造函数将能够看到

参数,该参数以与Form1.myClass1相同的值开始,但

您实际上不能参考来自Form2的Form1.myClass1变量。


< snip>

Certainly if you want to pass information from one context to a new
object, using a parameterised constructor is the way to go. That has
nothing to do with being able to access another type''s variables
though.

Just passing a parameter doesn''t mean that Form2 is magically able to
see Form1.myClass1. Form2''s constructor will be able to see the
parameter, which starts off with the same value as Form1.myClass1, but
you can''t actually refer to the Form1.myClass1 variable from Form2.

<snip>


重点是:如果需要前向参考Form1 {}中的Class1不是

''嵌套'',也就是说,如果Class1是单独的

翻译单元或模块的一部分,即使Class1是与

Form1相同的命名空间。
The point being: you need a forward reference if the Class1 is not
''nested'' in the Form1 {}, that is, if Class1 is part of a separate
translation unit or module, even if Class1 is in the same namespace as
Form1.



否。在C#中没有前向引用这样的东西,你认为是b $ b似乎是一个前向参考只需一个变量

声明。


-

Jon Skeet - < sk *** @ pobox.com>

网站: http://www.pobox.com/~双向飞碟

博客: http:// www .msmvps.com / jon.skeet

C#深度: http: //csharpindepth.com

No. There is no such thing as a forward reference in C#, and what you
seem to think is a forward reference is actually just a variable
declaration.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com


7月21日,1:34 * pm,Jon Skeet [C#MVP]< sk ... @ pobox.comwrote :
On Jul 21, 1:34*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:

raylopez99< raylope ... @ yahoo.comwrote:
raylopez99 <raylope...@yahoo.comwrote:

关键词:范围分辨率,在父和子之间传递类

表,参数构造方法,正常构造函数,默认

构造函数,转发引用,表单之间共享类。
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms.


这是我发现自己做的一个新手错误(作为一个新手),并且即使是一个主程序员,也是
,这个论坛的大师,Jon Skeet,

错过了! (他知道这一点我很有信心,但我认为这不是我的问题; LOL,我要他了)
Here is a newbie mistake that I found myself doing (as a newbie), and
that even a master programmer, the guru of this forum, Jon Skeet,
missed! (He knows this I''m sure, but just didn''t think this was my
problem; LOL, I am needling him)



我仍​​然认为这不是你的问题。你仍然感到困惑 - 并且

你从访问变量中改变了问题的更多内容。

信息传递。


I still don''t think it''s your problem. You''re still confused - and
what''s more you''ve changed the problem from "accessing a variable" to
"information passing".



不。那是我的问题。我的问题是我甚至看不到Form2中class1的
现在它已经解决了。你的问题(在你的脑海中)是完全另一个问题。

Nope. That was my problem. My problem was that I could not even see
the class1 in Form2 and now it''s solved. Your problem (in your mind''s
eye) is another problem altogether.


当然如果你想通过从一个上下文到新的

无关,只能访问另一种类型的变量


Certainly if you want to pass information from one context to a new
object, using a parameterised constructor is the way to go. That has
nothing to do with being able to access another type''s variables
though.



当然。


访问其他类型的变量与我的
$ b无关$ b问题,无论你是否承认。顺便说一下,我所有的变量

都是公开的,但是在我点击

参数化构造函数之前代码仍然没有工作。

Certainly.

And accessing another type''s variables has nothing to do with my
problem, whether you acknowledge it or not. BTW, all my variables
were set public and still the code did not work until I hit upon the
parameterised constructor.


>

只是传递参数并不意味着Form2神奇地能够获得
,请参阅Form1.myClass1。 Form2的构造函数将能够看到

参数,该参数以与Form1.myClass1相同的值开始,但

您实际上不能参考Form2中的Form1.myClass1变量。
>
Just passing a parameter doesn''t mean that Form2 is magically able to
see Form1.myClass1. Form2''s constructor will be able to see the
parameter, which starts off with the same value as Form1.myClass1, but
you can''t actually refer to the Form1.myClass1 variable from Form2.



你也可以。使用公共属性Get和Set,你肯定

可以引用Form1.myClass1变量,甚至私有变量,并且

我在我的代码中证明了这一点让我满意。你想看看
看一份副本吗?只需在C#上打开任何教科书,然后在获取下查看和

" Set"对于物业。


你让我想起那位在黑暗中丢失钥匙的大学教授,

并且徒劳地望着远离地方的路灯下他失去了他们的b $ b。当被问及他是否找到了钥匙时,他回答说,好吧,我这个没有丢失我的钥匙在这里,但这是我知道的唯一一个有$ b $的地方b light!哈哈,那是你的乔恩。你正在回答一个问题,我没有b
(也许我确实做过,'在你的脑海中''?)。

You can too. Using the public properties Get and Set, you certainly
can refer to the Form1.myClass1 variables, even private variables, and
I demonstrated this in my code to my satisfaction. Would you like to
see a copy? Just open any textbook on C# and look under "Get" and
"Set" for Properties.

You remind me of the college professor who lost his keys in the dark,
and kept vainly looking under a street lamp far away from where he
lost them. When asked if he found the keys, he replied, "well, I
didn''t lose my keys here, but this is the only place I know that has
light!" LOL, that''s you Jon. You''re answering a question that I didn''t
pose (or perhaps I did pose, ''in your mind''s eye''?).


>
>

重点是:*如果Class1不是

''嵌套''在Form1 {}中你需要一个前向引用,也就是说,如果Class1是单独的

翻译单元或模块的一部分,即使Class1与

Form1位于同一名称空间中。
The point being: *you need a forward reference if the Class1 is not
''nested'' in the Form1 {}, that is, if Class1 is part of a separate
translation unit or module, even if Class1 is in the same namespace as
Form1.



否。在C#中没有前向引用这样的东西,而你认为​​是b b $ b的前向引用实际上是什么只是一个变量

声明。


No. There is no such thing as a forward reference in C#, and what you
seem to think is a forward reference is actually just a variable
declaration.



好​​的,让我们将前向声明称为''变量声明''。一个

以任何其他名字上涨......


你是一个有趣的家伙。而你错了(但不是'在你的脑海中'')。


RL

OK, let''s call the forward declaration a ''variable declaration''. A
rose by any other name...

You''re a funny dude. And you''re wrong (but not ''in your mind''s eye'').

RL


raylopez99< ra ******** @ yahoo.comwrote:
raylopez99 <ra********@yahoo.comwrote:

访问其他类型的变量与我的

问题,无论你是否承认。
And accessing another type''s variables has nothing to do with my
problem, whether you acknowledge it or not.



它与你最初描述的问题有关,

有一个表格继承自另一个。但是,当你来到

显示实际代码时,你的表单都是从Form继承的。

It has everything to do with the problem you originally described,
which had one form inheriting from another. However, when you came to
show actually code your forms were both inheriting from Form.


BTW,我所有的变量

已公开,但在我点击

参数化构造函数之前,代码仍然无效。
BTW, all my variables
were set public and still the code did not work until I hit upon the
parameterised constructor.



的确,因为你的代码不是你最初的描述。你

声称继承关系根本不存在。

Indeed, because your code wasn''t how you originally described it. You
claimed an inheritance relationship which simply wasn''t there.


只是传递参数不并不意味着Form2神奇地能够获得
,请参阅Form1.myClass1。 Form2的构造函数将能够看到

参数,该参数以与Form1.myClass1相同的值开始,但

您实际上不能参考Form2中的Form1.myClass1变量。
Just passing a parameter doesn''t mean that Form2 is magically able to
see Form1.myClass1. Form2''s constructor will be able to see the
parameter, which starts off with the same value as Form1.myClass1, but
you can''t actually refer to the Form1.myClass1 variable from Form2.



你也可以。使用公共属性Get和Set,你肯定

可以引用Form1.myClass1变量,甚至私有变量,并且

我在我的代码中证明了这一点让我满意。你想看看
看一份副本吗?只需在C#上打开任何教科书,然后在获取下查看和

" Set"对于属性。

You can too. Using the public properties Get and Set, you certainly
can refer to the Form1.myClass1 variables, even private variables, and
I demonstrated this in my code to my satisfaction. Would you like to
see a copy? Just open any textbook on C# and look under "Get" and
"Set" for Properties.



这并不是指变量。这是指

属性,它们会在返回当前变量值时发生。大

的区别。尝试通过引用传递属性,例如...

That''s not referring to the variables. That''s referring to the
properties, which happen to the return the current variable value. Big
difference. Try passing the property by reference, for example...


你让我想起在黑暗中丢失钥匙的大学教授,

并且远远地望着一盏街灯远远地看着他失去了他们的地方。当被问及他是否找到了钥匙时,他回答说,好吧,我这个没有丢失我的钥匙在这里,但这是我知道的唯一一个有$ b $的地方b light!哈哈,那是你的乔恩。你正在回答一个我没有做过的问题(也许我确实在做了',在你的脑海中''?)。
You remind me of the college professor who lost his keys in the dark,
and kept vainly looking under a street lamp far away from where he
lost them. When asked if he found the keys, he replied, "well, I
didn''t lose my keys here, but this is the only place I know that has
light!" LOL, that''s you Jon. You''re answering a question that I didn''t
pose (or perhaps I did pose, ''in your mind''s eye''?).



不,我正在回答你*做*构成的问题,即使它可能没有

反映现实。你反复声明孩子的表格是从父表格继承的 - > b $ b,但是你的代码却表示不然。

这让我感到困惑 - 我愚蠢地相信你准确地描述了你的代码。


所以是的,我会因为没有解决你的问题而承担部分责任

之前 - 但你也应该接受你的部分内容,因为它首先错误地描述了它。< br>

No, I''m answering the question you *did* pose, even though it may not
have reflected reality. You repeatedly stated that the child form
inherited from the parent form - but then your code showed otherwise.
That''s what confused me - I foolishly trusted that you were accurately
describing your code.

So yes, I''ll take part of the blame for not solving your problem
earlier - but you should also accept your portion of it for
misdescribing it in the first place.


重点是:*如果Class1不是
,你需要一个前向引用$ 1 $ b''嵌套''在Form1 {}中,也就是说,如果Class1是单独的

翻译单元或模块的一部分,即使Class1与

Form1。
The point being: *you need a forward reference if the Class1 is not
''nested'' in the Form1 {}, that is, if Class1 is part of a separate
translation unit or module, even if Class1 is in the same namespace as
Form1.



否。在C#中没有前向引用这样的东西,你认为是b $ b似乎是一个前向参考只是一个变量

声明。

No. There is no such thing as a forward reference in C#, and what you
seem to think is a forward reference is actually just a variable
declaration.



好​​的,让我们将前向声明称为''变量声明''。 A

以其他任何名字上涨...

OK, let''s call the forward declaration a ''variable declaration''. A
rose by any other name...



嗯,不。 C ++中的前向声明与

变量声明完全不同。

Um, no. A forward declaration in C++ is not the same thing as a
variable declaration at all.


你是一个有趣的家伙。你错了(但不是'在你的脑海里'')。
You''re a funny dude. And you''re wrong (but not ''in your mind''s eye'').



好​​吧,你相信。当你拒绝使用正确的术语,错误描述你的b $ b b情况,然后声称比他们更了解时,不要指望人们继续努力帮助你。尽管对于语言的运作方式做出了野蛮的声明。我喜欢教学和

教练,这就是我参加这里的原因 - 但它必须是一个两个

方式的过程。

-

Jon Skeet - < sk *** @ pobox.com>

网站: http://www.pobox.com/~skeet

博客: http://www.msmvps.com/jon.skeet

C#深度: http://csharpindepth.com

Fine, you believe that. Just don''t expect people to keep trying to help
you when you refuse to use correct terminology, misdescribe your
situation, and then claim to know better than they do despite making
wild claims about how the language works. I enjoy teaching and
coaching, which is why I participate here - but it''s got to be a two-
way process.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

这篇关于如何在Windows Applicationmode中传递表单之间的信息,类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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