简体   繁体   English

我设计中的文字标签文字不会更改

[英]The text label's text in my design wont change

I have a text label inside of Form1 named txtOn . 我在Form1内有一个名为txtOn的文本标签。 I have made its modifier to public . 我已将其修饰语public Now I attempt to change the text using the following code with the click of a button. 现在,我尝试单击以下按钮,使用以下代码更改文本。 Nothing happens, but button clicked! 什么也没发生,但是单击了按钮! is logged to debug. 记录调试。

How can I make the text of the text label change successfully? 如何使文本标签的文本成功更改?

  private void button1_Click(object sender, EventArgs e)
    {
        Form1 home = new Form1();
        home.txtOn.Text = "test!";

        System.Diagnostics.Debug.WriteLine("button clicked!");
    }

You probably want either 您可能想要

// Start NEW Form1, show it and change the button
private void button1_Click(object sender, EventArgs e)
{
    Form1 home = new Form1();

    home.txtOn.Text = "test!";
    home.Show(); // <- do not forget to show the form

    System.Diagnostics.Debug.WriteLine("button clicked!");
}

or 要么

// On the EXISTING Form1 instance change the button
// providing that "button1_Click" method belongs to "Form1" class
private void button1_Click(object sender, EventArgs e)
{
    txtOn.Text = "test!";

    System.Diagnostics.Debug.WriteLine("button clicked!");
}

Is not working because you are re instantiating Form1 when it's already available. 无法正常工作,因为您已经在实例化Form1时将其实例化。 You are changing the text of a button not on the same instance as the one on the ui 您正在更改与UI不在同一实例上的按钮的文本

Try 尝试

private void button1_Click(object sender, EventArgs e) 
{ 

        txtOn.Text = "test!";

}

.

NO, change that labels text in your Form load or initialize method like 不,更改在表单加载中标记文本的标签或初始化方法,例如

private void button1_Click(object sender, EventArgs e)
    {
        Form1 home = new Form1();
        home.Show();     
        System.Diagnostics.Debug.WriteLine("button clicked!");
    }

Public Form1()
{
  txtOn.Text = "test!";
}

Assuming you are trying to open a different form altogether. 假设您正尝试完全打开其他表单。 If it's the same form then you don't need to create a separate form instance at all. 如果是相同的表单,则根本不需要创建单独的表单实例。 You can just say txtOn.Text = "test!"; 您可以说txtOn.Text = "test!";

You must create Form1 only once. 您只能创建一次Form1。 Sample 样品

`Form1 home = new Form1();
home.button1.Click += new System.EventHandler(this.button1_Click);

private void button1_Click(object sender, EventArgs e)
    {

        home.txtOn.Text = "test!";

        System.Diagnostics.Debug.WriteLine("button clicked!");
    }`

It is because you haven't loaded the new instance of form1. 这是因为您尚未加载form1的新实例。

Form1 home = new Form1();
home.show();
home.txton.text="test!";

If you want to change it on the current instance of the form you will need to use the this keyword: 如果要在表单的当前实例上进行更改,则需要使用this关键字:

this.txton.text="test!";

You need to expose your label or its property. 您需要公开标签或其属性。

In form 2 在表格2中

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

Then you can do: 然后,您可以执行以下操作:

form2 frm2 = new form2();
frm2.LabelText = "test";

You are creating one more object of the same form again. 您将再创建一个相同形式的对象。 So the label not getting changed. 因此标签不会更改。 Try the code bellow. 尝试下面的代码。

private void button1_Click(object sender, EventArgs e)
    {
        txtOn.Text = "test!";

        System.Diagnostics.Debug.WriteLine("button clicked!");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM