单击按钮更改标签文本 [英] changing label text with button click

查看:112
本文介绍了单击按钮更改标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在按钮单击时更改标签的文本,但它不起作用并给我运行时错误.我为LABEL有一个单独的类,为BUTTON有一个单独的类.这是伪代码.真实的代码包含位置标签和按钮的大小.一切都是动态创建的.谢谢!

I need to change the text of label on the button click, but it's not working and giving me run time error.I've got separate class for LABEL and separate for BUTTON.This is dummy code.the real code contains locations and sizes for both label and button.Everything is created dynamically. Thanks!

   /-------------------------------------LABEL class-------------------------------/
   private Label label1;

   public Label getLabel1()
   {
       return label1;
   }

   public LABEL()
   {
       label1 = new Label();
   }

    public void print()
    {            
        label1.Text = "x";
        Controls.Add(label1);
    }//

  /-------------------------------------BUTTON class----------------------------------/
    private Button button1;

    public BUTTON()
    {

    }

    public void print()
    {
        button1 = new Button();
        button1.Click +=new EventHandler(button1_Click);
        Controls.Add(button1);
    }

     public void button1_Click(object sender, EventArgs e)
     {
        LABEL label = new LABEL();
        label.getLabel1().Text = "y";
     }

推荐答案

您可以简单地尝试一下.

You can simply try it.

private void button1_Click(object sender, EventArgs e){label1.Text = "Hi";label1.Refresh();}

这篇关于单击按钮更改标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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