以另一种形式更改label.Text [英] Change label.Text in another form

查看:97
本文介绍了以另一种形式更改label.Text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表格。在Form1中我有一个标签,在Form2中我有一个按钮。当我在Form2中按下按钮时,我希望Form1中的标签将文本更改为新文本。



我应该怎么写这个?

I have 2 forms. In Form1 I have a label and in Form2 I have a button. When I press the button in Form2, I want the label in Form1 to change text to "new text".

How exactly should i write this?

推荐答案

实现起来很简单:

1)使用表单构造函数

Form2代码:

It''s simple to achieve:
1) using form constructor
Form2 code:
public partial class Form2 : Form
{
    //changed contructor:
    public Form2(string MyLabelText )
    {
        InitializeComponent();
        this.label1.Text = MyLabelText;
    }

}



Form1代码:


Form1 code:

private void button1_Click(object sender, EventArgs e)
{
    //
    Form2 frm = new Form2("new text for Label1");
    frm.Show();
}





2)使用代表

使用委托在两个表单之间传递数据 [ ^ ]

代表和活动 [ ^ ]

在Windows窗体之间传递数据 [ ^ ]



2) using delegates
Using a delegate to pass data between two forms[^]
Delegates and Events[^]
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


如果场景是Form1有标签而Form2有一个按钮可以改变标签的文字,然后当你加载Form2时,你必须使自定义构造函数发送Form1'的瞬间,以便它可以被操作。



例如,在Form1的构造函数中(我不知道你将如何加载Form2,我在form1中加载它''的建设者只是为了告诉你):



If the scenario is such that Form1 has a label and Form2 has a button that will change the label''s text, then while you are loading Form2 you must make a custom constructor sending Form1''s instant so that it can be manipulated.

For Example, inside Form1''s constructor (I don''t know how you will load Form2, I am loading it inside form1''s constructor just to show you):

public Form1()
{
     Form2 form2 = new Form2(this);
     form2.Show();
}





然后在Form2类中创建一个Form1类型的公共变量,以便所有方法都可以访问它。创建一个自定义构造函数,该构造函数接收该值并将其分配给全局变量。然后在按钮单击方法中操作该对象(假设标签在form1中称为label1,按钮在form1中称为button1):





Then inside Form2 class make a public variable of Form1 type so it can be accessed by all methods. Make a custom constructor that takes in that value and assigns it to the global variable. Then manipulate that object in the button click method(assuming the label is called label1 in form1 and button is called button1 in form1):

public class Form2
{
     public Form1 _form1;

     public Form2(Form1 form1)
     {
          _form1 = form1;
     }

     protected void button1_Click(object sender, EventArgs e)
     {
          //Assuming in Form1 label1 exists
          _form1.label1.Text = "New Text";
     }

}


这篇关于以另一种形式更改label.Text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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