Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#刷新动态标签上的值_C#_Label_Refresh_Controls - Fatal编程技术网

c#刷新动态标签上的值

c#刷新动态标签上的值,c#,label,refresh,controls,C#,Label,Refresh,Controls,我想问一个问题。我得到了这段代码,我的意图是让我的动态标签刷新值,而不必在标签上叠加另一个标签 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks;

我想问一个问题。我得到了这段代码,我的意图是让我的动态标签刷新值,而不必在标签上叠加另一个标签

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;   

namespace refresh_label
{
    public partial class Form1 : Form
    {
        Int64 num1 = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Timer t1 = new Timer();
            t1.Interval = 1;
            t1.Tick += new EventHandler(lbl_refresh);
            t1.Enabled = true;
            Label lbl = new Label();
            lbl.Text = num1.ToString();
            this.Controls.Add(lbl);
        }

        private void lbl_refresh(object sender, EventArgs e)
        {
            num1++;
        }
    }
}

//on this one it's working but staking labels. is there a way to refresh the label without staking it and then clear it? thank you for your time.

private void Form1_Load(object sender, EventArgs e)
{

    Timer t1 = new Timer();
    t1.Interval = 1;
    t1.Tick += new EventHandler(lbl_refresh);
    t1.Enabled = true;
}

private void lbl_refresh(object sender, EventArgs e)
{
    num1++;

    Label lbl = new Label();
    lbl.Text = num1.ToString();
    this.Controls.Add(lbl);
    lbl.BringToFront();
}
请试试这个-

在设计时添加标签并更改代码-

private void lbl_refresh(object sender, EventArgs e)
{
    num1++;

    lbl.Text = num1.ToString();
    lbl.Update();
}

首先,没有必要每次都创建一个新标签。您只是想创建一个计时器并在单个标签中显示值吗?您不能只使用lbl.Text=string.Empty;要清除,lbl.Text=newValue;设置,lbl.无效;要强制重画吗?您好,谢谢,已经尝试过了,但是它说:“lbl”这个名称在当前上下文中确实存在。我可以堆叠标签,清除标签,也可以用控件移除标签,但在很长一段时间内,我一直在堆叠标签,当我清除或移除标签时,它会产生闪烁,这是我的问题。在类范围内声明lbl(与您声明num1的位置相同),以便它在整个类中都可用。好的,它工作得很好:)非常感谢。它正在工作,不再堆叠。谢谢。你好,抱歉来晚了。有趣的是,它也很有效,不管怎样,它是解决问题的好方法。谢谢:)
Int64 num1 = 0;
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        Timer t1 = new Timer();
        t1.Interval = 1;
        t1.Tick += new EventHandler(lbl_refresh);
        t1.Enabled = true;
        Label lbl = new Label();

        lbl.Name="myLabel";
        lbl.Text = num1.ToString();
        this.Controls.Add(lbl);
    }

    private void lbl_refresh(object sender, EventArgs e)
    {
       Label l = Controls.Find("myLabel",true)[0] as Label;
       l.Text = num1.ToString();
       num1++;
     }