c#标签没有更新

时间:2014-11-15 08:56:17

标签: c# label updating

我在Panel中有Label。在timer.Tick新值写入Label.Text,但形式不变。

的Program.cs:

static void Main(string[] args)
{
    MainForm mainForm = new MainForm();
    Engine engine = new Engine(mainForm);
    Application.Run(mainForm);
}

MainForm.cs

public partial class MainForm : Form
{
    public MatchesPanel panel;

    public MainForm()
    {
        InitializeComponent();

        this.Load += MainForm_Load;

        this.panel = new MatchesPanel("panel", new System.Drawing.Point(0, 52));
        this.panel.Name = "panel";
        this.panel.TabIndex = 8;
        this.Controls.Add(this.panel);
    }
}

MatchesPanel.cs

public class MatchesPanel : Panel
{
    public string Name;
    int VWheelSize = 0;
    Dictionary<string, Label> Items = new Dictionary<string, Label>();
    Label InFocus = null;

    public MatchesPanel(string name, Point location)
    {
        Name = name;
        this.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
        this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.Location = location;
        this.Margin = new System.Windows.Forms.Padding(0);
        this.Size = new System.Drawing.Size(400, 323);
        this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)));

        this.MouseWheel += This_MouseWheel;
        this.Click += This_Click;
        this.Invalidated += MatchesPanel_Invalidated;
    }
    void Label_Click(object s, EventArgs e)
    {
        Label l = (Label)s;
        l.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        l.BackColor = System.Drawing.Color.MediumTurquoise;
        if (InFocus != null && InFocus != l)
        {
            InFocus.BorderStyle = System.Windows.Forms.BorderStyle.None;
            InFocus.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
        }
        InFocus = l;
        l.Focus();
    }
    public void SetText(string key, string text)
    {
        Label l = Items[key];
        l.Text = text;
    }
    public void Add(string key, string text)
    {
        Label label = new Label();

        label.AllowDrop = true;
        label.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
        label.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
        label.Location = new Point(0, Items.Count * 37 + VWheelSize);
        label.Margin = new System.Windows.Forms.Padding(0);
        label.Padding = new System.Windows.Forms.Padding(3);
        label.Size = new System.Drawing.Size(400, 37);
        label.Text = text;
        label.Click += Label_Click;
        label.LostFocus += Label_LostFocus;
        label.MouseLeave += Label_MouseLeave;
        label.MouseMove += Label_MouseMove;

        this.Controls.Add(label);
        Items.Add(key, label);
    }
    public void UpdateMatches(Dictionary<string, Match> Matches)
    {
        string newStr;

        // calculate newStr...
        // in debugger i check newStr value and this right

        // but text not updating
        this.SetText(key, newStr);
    }
}

Engine.cs

public class Engine
{
    System.Windows.Forms.Timer timer = null;

    public Dictionary<string, Match> Matches;

    MainForm MainForm = null;

    public Engine(MainForm mainForm)
    {
        MainForm = mainForm;

        Matches = new Dictionary<string, Match>();

        timer = new System.Windows.Forms.Timer();
        timer.Tick += timer_Tick;
        timer.Interval = 1000;
        timer.Start();
    }
    public void timer_Tick(object s, EventArgs e)
    {
        System.Windows.Forms.Timer timer = (System.Windows.Forms.Timer)s;
        timer.Stop();

        Proc();

        timer.Start();
    }
    public async void Proc()
    {
        // do something...
        MainForm.Invoke((System.Windows.Forms.MethodInvoker)delegate()
        {
            MainForm.panel.UpdateMatches(Matches);
        });
    }
}

我尝试使用:

Label.Update();
Label.Refresh();
Label.Invalidate();

但它不起作用。

如果我点击标签,当它没有聚焦时(InFocus!= clickLabel in clickHandler),文本值在sourceLabel中更新一次。 帮帮我吧我读了另一个主题而没有找到解决方案。 如果需要更多代码,请告诉我。 THX。

[编辑] 我简化了我的代码。

的Program.cs:

static void Main(string[] args)
{
    MainForm mainForm = new MainForm();
    Engine engine = new Engine(mainForm);
    Application.Run(mainForm);
}

MatchesPanel.cs

public class MatchesPanel : Panel
{
    Dictionary<string, Label> Items = new Dictionary<string, Label>();

    public MatchesPanel(Point location)
    {
        this.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
        this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.Location = location;
        this.Margin = new System.Windows.Forms.Padding(0);
        this.Size = new System.Drawing.Size(400, 323);
        this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)));

    }
    public void SetText(string key)
    {
        Label l = Items[key];
        l.Text = DateTime.Now.ToString();
    }
    public void Add(string key)
    {
        Label label = new Label();

        label.Name = key;
        label.AllowDrop = true;
        label.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
        label.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
        label.Location = new Point(0, Items.Count * 37 + VWheelSize);
        label.Margin = new System.Windows.Forms.Padding(0);
        label.Padding = new System.Windows.Forms.Padding(3);
        label.Size = new System.Drawing.Size(400, 37);
        label.Text = DateTime.Now.ToString();

        this.Controls.Add(label);
        Items.Add(key, label);
    }
}

Engine.cs

public class Engine
{
    System.Windows.Forms.Timer timer = null;

    public MatchesPanel panel = null;
    MainForm MainForm = null;

    public Engine(MainForm mainForm)
    {
        MainForm = mainForm;

        timer = new System.Windows.Forms.Timer();
        timer.Tick += timer_Tick;
        timer.Interval = 1000;
        timer.Start();
    }
    public void timer_Tick(object s, EventArgs e)
    {
        System.Windows.Forms.Timer timer = (System.Windows.Forms.Timer)s;
        timer.Stop();

        MainForm.Invoke((System.Windows.Forms.MethodInvoker)delegate()
        {
            if (panel == null)
            {
                panel = new MatchesPanel(new System.Drawing.Point(0, 52));
                panel.Name = "key1";

                // add label in main form
                panel.Add("key1");
                MainForm.Controls.Add(panel);
            }
            panel.SetText("key1");
        });

        timer.Start();
    }
}

1 个答案:

答案 0 :(得分:0)

问题:

Label.Text不更新。

解:

public class UpdatableLabel : Button
{
    public UpdatableLabel() : base()
    {
        FlatAppearance.BorderSize = 0;
        FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        Pen pen = new Pen(FlatAppearance.BorderColor, 1);
        Rectangle rectangle = new Rectangle(0, 0, Size.Width - 1, Size.Height - 1);
        e.Graphics.DrawRectangle(pen, rectangle);
    }
}

现在我的&#34;标签&#34;已更新。

相关问题