如何根据计时器更改图片框中的图像?

时间:2017-06-08 10:09:37

标签: c# windows timer

在我的用户控件中,我试图在计时器停止时更改PictureBox中的图像。但它只是第一次有效。如果我再次启动计时器,图像不会更新。我使用下面的代码来更改图像。

public partial class CANMsgLEDControl : UserControl
{
    #region Constants
    private const int kLedOn = 2;
    private const int kLedOff = 4;
    #endregion

    private bool _blinkEnabled = false;
    private int _blinkRate = 500;        
    private GridStyleInfo _currentCell = null;       
    private System.Timers.Timer _timer = new System.Timers.Timer();       


    public CANMsgLEDControl()
    { 
    }

    public CANMsgLEDControl(GridStyleInfo cell)  
    {
        InitializeComponent();
        picBxLed.Image = imageList1.Images[kLedOff];
        _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);

        _currentCell = cell;            
        if (_currentCell != null)
            this.BackColor = _currentCell.BackColor;

         Create Handle
        if (!this.IsHandleCreated)
        {
            CreateHandle();
        }
    }


    #region Properties

    [Browsable(true)]
    public bool BlinkEnabled
    {
        get { return _blinkEnabled; }
        set { _blinkEnabled = value; }
    }        

    [Browsable(true)]
    public int BlinkRate
    {
        get { return _blinkRate; }
        set 
        {
            _blinkRate = value;
            _timer.Interval = _blinkRate;
        }
    } 

    #endregion   

    public void ResetLedColor()
    {
        this.picBxLed.Image = imageList1.Images[kLedOff];
    }

    public void BlinkStart()
    {
        _currentCell.BeginUpdate();

        picBxLed.Image = imageList1.Images[kLedOn];
        _currentCell.EndUpdate();            
        _timer.Start();
    }

    #region Events        

    private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        if (InvokeRequired)
        {
            BeginInvoke(new ElapsedEventHandler(_timer_Elapsed), new object[] { sender, e });
        }
        else
        {
            if (this.BlinkEnabled)
            {
                _timer.Stop();                    

                _currentCell.BeginUpdate();
                picBxLed.Image = imageList1.Images[kLedOff];                     
                _currentCell.EndUpdate();
            }
        }           
    }  

    #endregion

}

定时器停止后如何更改图像?任何人都可以建议一种方法来克服这个问题吗?

此致 Arulpriya

0 个答案:

没有答案