2015-05-07 15 views
8

Tôi đã tạo một lớp BlinkingLabel, xuất phát từ Forms.Label, có một số Forms.Timer cho phép tôi bật và tắt hiệu ứng nhấp nháy.Đồng bộ hóa các nhãn nhấp nháy trong C#

Tôi đã tạo 4 nhãn loại BlinkingLabel, vấn đề của tôi là nếu tất cả 4 nhãn nơi nhấp nháy trong các thời điểm khác nhau, hiệu ứng nhấp nháy không được đồng bộ hóa.

Làm cách nào để điều chỉnh thiết kế theo cách mà ngay cả khi các nhãn nhấp nháy vào các thời điểm khác nhau, thì nhấp nháy sẽ được đồng bộ hóa?

******* Đã chỉnh sửa ****** Tôi đã thêm mã sau đây, nhưng tôi vẫn không thể nhận được nhãn 1 và 2 để nhấp nháy cùng một lúc. Những gì đang cố gắng làm là để kiểm tra như sau: làm cho label1 nhấp nháy sau đó tôi nhấp vào nút để làm cho nhãn 2 nhấp nháy và chúng không được đồng bộ hóa.

Bất kỳ ý tưởng nào đang làm sai?

public partial class UserControl1 : UserControl 
{ 
    Timer blinkTimer; 
    Color blinkingColor = Color.Red; 
    int interval = 300; 

    bool flag1 = false; 
    bool flag2 = false; 

    public UserControl1() 
    { 
     InitializeComponent();  // Blinking abel default values 
     this.blinkTimer = new Timer(); 
     this.blinkTimer.Interval = interval; ; 
     this.blinkTimer.Tick += new System.EventHandler(timer_Tick); 
     flag1 = true; 
     this.blinkTimer.Start(); 
    } 

    private void blinkLabels(Label label) 
    { 
     if (label.ForeColor == Color.White) 
      label.ForeColor = blinkingColor; 
     else 
      label.ForeColor = Color.White; 
    } 

    void timer_Tick(object sender, System.EventArgs e) 
    { 
     if(flag1 == true) 
      blinkLabels(label1); 
     if(flag2 == true) 
      blinkLabels(label2); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     flag2 = true; 
     this.blinkTimer.Start(); 

    } 
+1

Sử dụng một bộ hẹn giờ cho tất cả các nhãn. Bạn có thể ghi đè lên nhãn của mình để cho phép bạn kiểm soát nhấp nháy từ một lớp có thể xem/hiển thị tất cả các nhãn. Sau đó, sử dụng một bộ đếm thời gian trong lớp đó để làm cho tất cả chúng nhấp nháy. – deathismyfriend

+0

.. bạn vẫn có thể đặt cờ trên mỗi nhãn để làm cho nó tham gia là lễ hội hay không .. – TaW

+0

@deathismyfriend vui lòng xem mã thử nghiệm của tôi. cảm ơn bạn. – newbieLinuxCpp

Trả lời

12

Bạn cần phải sử dụng chỉ một Timer và làm cho nó nhấp nháy tất cả nhãn. Điều này được thực hiện một cách tao nhã nhất bằng cách tạo một thành phần thực hiện giao diện IExtenderProvider. Tương tự như cách ErrorProvider hoạt động, tôi sẽ hiển thị cho bạn một thuộc tính Blink cho mọi điều khiển Nhãn trên biểu mẫu.

Thêm lớp mới vào dự án của bạn và dán mã được hiển thị bên dưới. Biên dịch. Thả thành phần mới từ đầu hộp công cụ lên biểu mẫu của bạn. Đặt thuộc tính Blink cho bất kỳ nhãn nào cần nhấp nháy thành True.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Windows.Forms; 

[ProvideProperty("Blink", typeof(Label))] 
public class BlinkProvider : Component, IExtenderProvider { 
    public BlinkProvider() { 
     timer = new Timer(); 
     timer.Tick += timer_Tick; 
     Interval = 500; 
     Enabled = true; 
     BlinkingColor = Color.Red; 
    } 
    public BlinkProvider(IContainer container) 
     : this() { 
     container.Add(this); 
    } 

    protected override void Dispose(bool disposing) { 
     if (disposing) timer.Dispose(); 
     base.Dispose(disposing); 
    } 

    [DefaultValue(500)] 
    public int Interval { 
     get { return timer.Interval; } 
     set { timer.Interval = value; } 
    } 

    [DefaultValue(true)] 
    public bool Enabled { get; set; } 

    [DefaultValue(typeof(Color), "255, 255, 0, 0")] 
    public Color BlinkingColor { 
     get; 
     set; 
    } 

    private void timer_Tick(object sender, EventArgs e) { 
     if (this.DesignMode) return; 
     tock = !tock; 
     foreach (var lbl in labels.Keys) { 
      if (labels[lbl].Blink && Enabled && tock) lbl.ForeColor = BlinkingColor; 
      else lbl.ForeColor = labels[lbl].ForeColor; 
     } 
    } 

    bool IExtenderProvider.CanExtend(object extendee) { 
     return extendee is Label; 
    } 
    public bool GetBlink(Label label) { 
     AddLabelIfNecessary(label); 
     return labels[label].Blink; 
    } 
    public void SetBlink(Label label, bool blink) { 
     AddLabelIfNecessary(label); 
     labels[label].Blink = blink; 
    } 
    private void AddLabelIfNecessary(Label label) { 
     if (!labels.ContainsKey(label)) { 
      labels.Add(label, new BlinkInfo { Blink = false, ForeColor = label.ForeColor }); 
     } 
     timer.Enabled = !DesignMode; 
    } 

    private Timer timer; 
    private bool tock; 
    private class BlinkInfo { 
     public Color ForeColor; 
     public bool Blink; 
    } 
    private Dictionary<Label, BlinkInfo> labels = new Dictionary<Label, BlinkInfo>(); 
} 
4

chúng thay đổi màu cùng một lúc, nhưng chúng không được đồng bộ hóa. để đồng bộ hóa chúng và sử dụng cùng một màu. Bạn cần phải thay đổi mã để các màu chớp được giữ bởi bộ đếm thời gian hoặc sự kiểm soát không phải là nhãn thử một cái gì đó như thế này

Color currentBlink;  
private void blinkLabels(Label label) 
{ 
      label.ForeColor = currentBlink;   
} 

void timer_Tick(object sender, System.EventArgs e) 
    { 
     if (currentBlink== Color.White) 
      currentBlink = blinkingColor; 
     else 
      currentBlink = Color.White; 


     if(flag1 == true) 
      blinkLabels(label1); 
     if(flag2 == true) 
      blinkLabels(label2); 
    } 
Các vấn đề liên quan