【问题标题】:Changing label text in Form2 after pressing a button in Form1在 Form1 中按下按钮后更改 Form2 中的标签文本
【发布时间】:2016-12-14 11:35:23
【问题描述】:

在Form1中单击按钮后如何更改Form2中的标签文本?

例如,如果我在 Form1 中按下 button1 并且如果我在 Form1 中按下 button2,我希望 Form2 中的标签文本更改为“按下按钮 1”,它将 是“按钮 2 被按下”。

注意: Form1 和 Form2 不会同时显示。所以我必须单击按钮,然后 Form2 将显示更新的标签文本。

【问题讨论】:

  • 你能告诉我们你到目前为止所做的尝试吗?
  • 发帖前请先探索一下..

标签: c# forms winforms label


【解决方案1】:

您可以在 Form1 类上添加一个单击按钮 1 的事件

private void button1_Click(object sender, EventArgs e)
        {
            Form2 form= new Form2();
            form.Show();
            // if you want to hide form1
            // this.Hide();
            form.label1.Text = "Hello World";            
        }

但在此之前,您应该在 Form2.Designer.cs 上将您的 label1 标记为公开:

public System.Windows.Forms.Label label1;

【讨论】:

  • 谢谢!终于让它按照我想要的方式工作。 :)
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多