在C#中的另一种形式更改标签的文本? [英] Changing a label's text in another form in C#?

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

问题描述

我有一个名为LabelX1标签。这是窗口2。在Form1上,我有一个按钮。我想按钮的文本到被转移到其它形式的标签。我曾尝试

I have a label called LabelX1. This is on form2. On form1, i have a button. I want the button's text to be transferred to the other form's label. I have tried

form2 frm2 = new form2();
frm2.labelX1.Text = this.button1.text;

但它不工作。是否有这样做的一个简单,直接的方式?

But it does not work. Is there an easy, straight forward way of doing this?

推荐答案

您需要公开您的标签或其属性。

You need to expose your label or its property.

在表格2:

public string LabelText
{
    get
    {
        return this.labelX1.Text;
    }
    set
    {
        this.labelX1.Text = value;
    }
}

然后,你可以这样做:

Then you can do:

form2 frm2 = new form2();
frm2.LabelText = this.button1.text;

这篇关于在C#中的另一种形式更改标签的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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