简体   繁体   English

更改C#中的label.text

[英]change the label.text in C#

I am a newbie with C#. 我是C#的新手。 I am trying to show how much times the button is clicked, here is my code below: 我试图显示单击按钮的次数,这是下面的代码:

private void ClickMebutton_Click(object sender, EventArgs e)
{
      int i = 0;     
      while (true)    
       {              
         label1.Text = i.ToString();
         i += 1;   
       }
}

code runs fine, but label.text is not changing when I clicked the button. 代码运行正常,但是当我单击按钮时,label.text并未更改。 I also cannot identify any problem by using break points. 我也无法通过使用断点来识别任何问题。 Can anyone please help me out? 有人可以帮我吗?

int i = 0; // 1

private void ClickMebutton_Click(object sender, EventArgs e)
{
    i++; // 3
    label1.Text = i.ToString();
}

Comments: 评论:

  1. Store counter outside of event handler body. 将计数器存储在事件处理程序主体外部。 Otherwise, it will reset to 0 each time you click on the button. 否则,每次单击该按钮时,它将重置为0 You can also parse label text instead of storing counter in the class field. 您也可以解析标签文本,而不是将计数器存储在类字段中。
  2. Remove infinite while loop 删除无限的while循环
  3. Increment counter before you assign text. 分配文本之前先增加计数器。 Otherwise, after first click you will have 0 否则,第一次点击后,您将获得0

Solution with parsing label text: 解析标签文本的解决方案:

Note that initially you should set label text to "0" . 请注意,最初应将标签文本设置为"0" Do it in the designer. 在设计器中完成。 Thus after application start label will show correct information - button was clicked zero times. 因此,应用程序启动后标签将显示正确的信息-单击按钮零次。

private void ClickMebutton_Click(object sender, EventArgs e)
{
    int i = Int32.Parse(label1.Text);
    i++;
    label1.Text = i.ToString();
}

you should define i publicly 你应该公开定义我

 private int i = 0;     
private void ClickMebutton_Click(object sender, EventArgs e)
{

     i++;          
     label1.Text = i.ToString();


 }

It seems that it is not changing since your are having an infinite loop here: 似乎没有改变,因为您在这里遇到无限循环:

 while (true)

If you put your break point on that spot , you are just looping and looping that's why you cannot see the changes. 如果您将断点放在该位置上 ,那么您就是在循环播放,这就是为什么看不到更改的原因。 Make this: 做这个:

int i = 0;

A global variable, and remove your infinite loop which you don't really need on your logic: 全局变量,并删除逻辑上实际上不需要的无限循环:

private int i = 0;     
private void ClickMebutton_Click(object sender, EventArgs e){   

  i++;
  label1.Text = i.ToString();
 }

Remove infinite loop ( while (true) ), but add 1 to the label1.Text : 删除无限循环while (true) ),但将1添加到label1.Text

private void ClickMebutton_Click(object sender, EventArgs e) {
  int value;

  if (int.TryParse(label1.Text, out value))
    label1.Text = (value + 1).ToString();
  else
    label1.Text = "1";
}

In case of C# 7.0+ you can put it terser, but less readable: C#7.0+的情况下,您可以放下它,但可读性较差:

private void ClickMebutton_Click(object sender, EventArgs e) {
  label1.Text = (int.TryParse(label1.Text, out var value) ? value + 1 : 1).ToString();
}

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

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