如何为进度条提升事件(初学者)

本文关键字:事件 初学者 | 更新日期: 2023-09-27 18:31:28

我想

在进度栏为 0 或 100 时上升一个事件,但是尽管我付出了努力,但我还没有在 Value Property 的进度栏事件中找到这样的事件,我想知道我如何自己实现这样的事件?

我在哪里声明此类事件?

如何为进度条提升事件(初学者)

ProgressBar只是一个indicator,所以它不支持某些ValueChanged事件。但是,您可以自定义它以支持此类事件和所需的其他类似事件:

    public class CustomProgressBar : ProgressBar
    {
        public event EventHandler ReachedMinimum;
        public event EventHandler ReachedMaximum;
        public event EventHandler ValueChanged;            
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0x402)//PBM_SETPOS = WM_USER + 2
            {     
               EventHandler handler = ValueChanged;
               if(handler != null) handler(this, EventArgs.Empty);
               handler = ReachedMinimum;                    
               if (Value == Minimum && handler!=null) handler(this, EventArgs.Empty);
               handler = ReachedMaximum;
               if (Value == Maximum && handler != null) handler(this, EventArgs.Empty);
            } 
        }
    }
    //Use it
    customProgressBar1.ReachedMaximum += (s,e) => {
          MessageBox.Show("Reached maximum!");
    };
    //... the same for other events