How to get datetimepicker c# winform checked/unchecked event

前端 未结 4 1667
梦毁少年i
梦毁少年i 2021-02-19 04:15

There is a check box in the datetimepicker control of winforms .net. But I could not find the event that is triggered when the check box is checked or unchecked . Is there a way

4条回答
  •  不要未来只要你来
    2021-02-19 04:56

    Run into the same issue. I needed a CheckedChangedEvent on a winforms DateTimePicker control. So with the inspiration of the answers before me I created an inherited User Control named DateTimePicker2, inheriting from DateTimePicker that implements this event. It looks like it works but no guarantees.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace MyNameSpace
    {
        public partial class DateTimePicker2 : DateTimePicker
        {
            private bool _checked;
    
            public new bool Checked
            {
                get
                {
                    return base.Checked;
                }
                set
                {
                    if (value != base.Checked)
                    {
                        base.Checked = value;
                        _checked = base.Checked;
                        OnCheckedChanged(new CheckedChangedEventArgs { OldCheckedValue = !value, NewCheckedValue = value });
                    }
                }
            }
    
            public event CheckedChangedEventHandler CheckedChanged;
    
            public DateTimePicker2()
            {
                InitializeComponent();
                _checked = Checked;
            }
    
    
            protected virtual void OnCheckedChanged(CheckedChangedEventArgs e)
            {
                if (CheckedChanged != null) CheckedChanged(this, e);
            }
    
            private void DateTimePicker2_ValueChanged(object sender, EventArgs e)
            {
                if (Checked != _checked)
                {
                    _checked = Checked;
                    CheckedChangedEventArgs cce = new CheckedChangedEventArgs { OldCheckedValue = !_checked, NewCheckedValue = _checked };
                    OnCheckedChanged(cce);
                }
            } 
        }
    
        public delegate void CheckedChangedEventHandler(object sender, CheckedChangedEventArgs e);
    
        public class CheckedChangedEventArgs : EventArgs
        {
            public bool OldCheckedValue { get; set; }
            public bool NewCheckedValue { get; set; }
        }
    
    }
    

    And off-course don't forget to subscribe to the DateTimePicker2_ValueChanged event from the designer.

    The reason why I used both a new Checked property (to hide the base.Checked one) and a _checked field to keep truck of the old value, is because

    1. the base.Checked property does not fire the ValueChanged event when changed programmatically and therefore needed a new property that could do that.
    2. the this.Checked new property does not fire the ValueChanged event when changed from the UI and therefore needed a flag that would track the base.Checked property.

    Basically a combination of both approaches was needed.

    I hope this helps.

提交回复
热议问题