How to get datetimepicker c# winform checked/unchecked event

前端 未结 4 1510
梦毁少年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

    It does however trigger the value changed event

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-19 05:03

    I know this is super old but this could help someone.

    You can capture DataTimePicker.MouseUp event

    private void dateTimePicker1_MouseUp(object sender, MouseEventArgs e)
        {
            if (((DateTimePicker)sender).Checked)
            {
                //Do whatever you need to do when the check box gets clicked
            }
            else
            {
                //Do another stuff...
            }
        }
    

    You will need to do the same with KeyUp event in order to get the Space key press that could also activate the checkbox.

    0 讨论(0)
  • 2021-02-19 05:05

    You´ll have to store the old "checked" value in order to compare to the new one, so you´ll be able to determine if the "checked" state has changed:

    bool oldDateChecked = false; //if it's created as not checked
    
    private void dtp_filtro_date_ValueChanged(object sender, EventArgs e)
    {
        if (this.dtp_filtro_date.Checked != oldDateChecked)
        {
            oldDateChecked = this.dtp_filtro_date.Checked;
            //do your stuff ...
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题