2014-11-15 85 views
0

我有標籤的面板。在timer.Tick新值寫入Label.Text中,但形式不變。C#標籤不更新

的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(); 

但它不起作用。

如果我在標籤上單擊,當它沒有焦點(富可視!= sourceLabel在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(); 
    } 
} 
+2

「如果需要更多的代碼,告訴我」 - 不,你需要_less_代碼。坦率地說,你應該自己調試一下。在調用'UpdateMatches()'時設置一個斷點,看看實際發生了什麼。但是如果你需要幫助,你需要發佈一個代碼示例,除了最小化重現行爲所需的最低限度外,不包含任何內容。見http://stackoverflow.com/help/mcve(順便說一句,你似乎被濫用'BackgroundWorker' ...你應該返回'Matches'(無論是),讓'RunWorkerCompleted'事件處理程序調用'UpdateMatches ()')。 –

+0

我刪除了bgWorker。 UpdateMatches使用正確的文本進行調用。在SetText()文本中也是如此。 – user3424037

+0

我簡化了代碼 – user3424037

回答

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

現在我的 「標籤」 被更新。