c#动态标签上的刷新值

时间:2016-09-01 20:09:57

标签: 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.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();
}

2 个答案:

答案 0 :(得分:1)

请试试这个 -

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

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

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

答案 1 :(得分:0)

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++;
     }