C#Form1数据到Form2 [英] C# Form1 data to Form2

查看:95
本文介绍了C#Form1数据到Form2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,Form1有一个按钮来打开Form2。在Form2中,我需要一个标签字段的值(让我们称之为lblProductID)。当用户单击TreeView项时,lblProductID会被填充,并且TreeView本身会从数据库中的结果中填充。



我可以访问标签控件(lblProductID)在Form2中的Form1中,但值始终为空。我一直在挖掘如何完成这个看似简单的任务的好几个小时,这就是我找到的。



在Form2中我有......



Form1 myForm = new Form1;

string myString = myForm.lblProductID.Text;



我调试了以查看值,我注意到所有动态填充的标签都有空值,即使我可以在Form1中清楚地看到它们。我注意到静态标签值是在调试模式下填充的。



所以我的问题是,如何将Form1上标签的动态填充值转换为Form2?



谢谢。

I have a situation where Form1 has a button to open Form2. In Form2, I need the value of one of the label fields (let's call it lblProductID). lblProductID gets populated when a user clicks on a TreeView item, and the TreeView itself gets populated from the results in a database.

I can access the label control (lblProductID) in Form1 from Form2, but the value is always blank. I have been digging for many hours on how to accomplish this seemingly simple task, and this is what I found.

In Form2 I have...

Form1 myForm = new Form1;
string myString = myForm.lblProductID.Text;

I debugged to look at the values and I noticed that all of the dynamically populated labels have blank values, even though I can clearly see them in Form1. I did notice that static label values are populated in debug mode.

So my question is, how do I get the dynamically populated value of a label on Form1 into Form2?

Thanks.

推荐答案

注意:你的问题中缺少的一个关键部分是:哪个表格是TreeView上;假设它在Form1上是合乎逻辑的,但要真正回答你的问题,你需要绝对确定。



问题的本质你在这里问:我如何在表格之间交换信息,这是最常见的问题之一,并回答了有关CP Q& A的问题。



有几个有效的策略,包括将引用注入到Forms的实例中(一个Form实例引用另一个Form的实例);有一个Form定义,并引发事件,另一个Form(有权访问Form的实例)订阅,并在一个Form上公开属性,这样:当你创建它的一个实例时,另一个Form有一个引用它的实例,因此可以访问另一个Form实例的公共属性。



在Form2的代码中:你创建一个 Form1的新实例:
Note: a critical piece missing in your question is: which Form is the TreeView on; it's kind-of logical to assume it's on Form1, but to really answer your question, you need to be absolutely sure of that.

The "essence" of the question you ask here: "how can I exchange information between Forms," is one of the most commonly asked, and answered questions on CP Q&A.

There are several valid strategies, including injection of references into instances of Forms (one Form instance has a reference to another Form's instance); having one Form define, and raise Events, that another Form (that has access to the instance of the Form) subscribes to, and exposure of Public properties on one Form, so that: when you create an instance of it, another Form has a reference to the instance of it, and can, thus, access the public properties of the other Form instance.

In your code for Form2: you create a new instance of Form1:
Form1 myForm = new Form1;
string myString = myForm.lblProductID.Text;

:它是空白,空白,'myForm上的'lblProductID控件默认情况下(不应更改)定义为'私有:你无法访问它。



由于你的Form1 的代码创建了Form2的实例,所以Form1可以保留一个参考创建Form2的实例:

: it's "blank," empty, and the 'lblProductID Control on 'myForm is, by default (and should not be changed), defined as 'private: you can't access it.

Since your code for Form1 creates the instance of Form2, so Form1 can keep a reference to the instance of Form2 created:

private Form2 InstanceOfForm2;

private void Form1_Load(object sender, EventArgs e)
{
    InstanceOfForm2 = new Form2();
}

但是,你的Form1实例如何使Form2的实例让一个Control根据Form1上的Label设置一些值:嗯,一种方法是创建一个公共Form2上的属性,可以允许Form1设置它,并导致该属性的设置更改Form2上显示的内容。



例如,假设你放了一个Form2上的TextBox'textBox1,并将此属性添加到Form2的类定义:

But, how does your instance of Form1 cause the instance of Form2 to have a Control to have some value set based on the Label on Form1: Well, one way is to create a public property on Form2 that can allow Form1 to set it, and to cause the setting of that property to change what is presented on Form2.

For example, suppose you put a TextBox 'textBox1 on Form2, and you add this property to Form2's class definition:

public string Form2TextBox1Text
{
    get { return textBox1.Text; }
    set
    {
        textBox1.Text = value;
    }
}

现在,在Form1中,您可以执行以下操作:

Now, in Form1, you can do the following:

private Form2 InstanceOfForm2;

private void Form1_Load(object sender, EventArgs e)
{
    InstanceOfForm2 = new Form2();
    InstanceOfForm2.Form2TextBox1Text = lblProductID;
}

想想这里发生了什么:你只在Form2实例上暴露了'textBox1的Text属性,以便在Form1的实例上使用。而且,这就是你所做的一切:



1.所以,现在,Form1的一个实例可以读取,并写入Form2的Text属性'textBox1:at任何时候。



但是,现在,想想你所做的



1.如果用户在Form2上编辑了Form2的'textBox1 Text属性,并且您希望在Form1的实例中执行某些操作,则如果文本更改:您尚未启用



因此,如果你想对Form2上的某些内容进行更改以触发Form1上发生的事情:你将需要学习如何使用/实现事件和CodeProject充满了很好的资源,可以帮助您学习如何实现事件,并公开它们,因此在定义事件之外的对象(类,表单)可以订阅它们。



另一种说法是:对象实例中的公共事件允许通知订阅者的消费者那些Ev ents通过.NET的多播事件工具(基于代表)。



如上所述,还有许多其他技术:我们可以注入一个参考将Form1的实例转换为Form2(坏主意)。或者,可以给Form1的实例访问不仅仅是'Form2的'text属性'textBox1,而是TextBox本身。



第二个想法可能是合适的,因为:如果Form1可以访问Form2上的TextBox,它可以订阅TextBox的TextChanged事件,然后你就不必在Form2中编写自己的事件了。



我故意
在这种情况下没有使用第二种替代方案,因为我希望你注意你学习代表和事件的重要性。

Think about what's happened here: you have "exposed" only the Text property of 'textBox1 on the instance of Form2 for use on an instance of Form1. And, that's all you've done:

1. so, now, an instance of Form1 can read, and write to the Text property of Form2 'textBox1: at any time.

But, now, think about what you have not done:

1. If Form2's 'textBox1 Text property is edited on Form2, by the user, and you wanted to cause some action to happen in the instance of Form1, if the text changed: you have not enabled that.

So, if you want a change on something on Form2 to trigger something happening on Form1: you are going to need to learn how to use/implement Events, and CodeProject is full of good resources to help you learn how to implement Events, and "expose" them, so objects (Classes, Forms) outside where the Events are defined can subscribe to them.

Another way to say that is to say: that public Events in instances of objects enable notification of "consumers" of (subscribers to) those Events through .NET's multi-cast Event facility (which is based on Delegates).

As mentioned, there are many other techniques: we could have "injected" a reference to the instance of Form1 into Form2 (bad idea). Or, could have given the instance of Form1 access not to just the 'Text property of Form2's 'textBox1, but the TextBox itself.

That second idea might be appropriate, because: if Form1 has access to the TextBox on Form2, it could subscribe to the TextChanged Event of the TextBox, and then you would not have to write your own Event in Form2.

I deliberately did not use this second alternative in this case, because I hope to direct your attention to the great importance of your learning about Delegates, and Events.


基本方法..

在表单之间传递数据 [ ^ ]



更好的方法......

在Windows窗体之间传递数据 [ ^ ]
Basic approach..
Passing Data Between Forms[^]

Better approach...
Passing Data between Windows Forms[^]


这是关于表单协作的热门问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


这篇关于C#Form1数据到Form2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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