简体   繁体   English

如何获取 datetimepicker c# winform 选中/未选中事件

[英]How to get datetimepicker c# winform checked/unchecked event

There is a check box in the datetimepicker control of winforms .net. winforms .net的datetimepicker控件中有一个复选框。 But I could not find the event that is triggered when the check box is checked or unchecked.但是我找不到选中或取消选中复选框时触发的事件。 Is there a way out?有出路吗?

It does however trigger the value changed event但是它确实触发了值更改事件

Run into the same issue.遇到同样的问题。 I needed a CheckedChangedEvent on a winforms DateTimePicker control.我需要一个 Winforms DateTimePicker 控件上的 CheckedChangedEvent。 So with the inspiration of the answers before me I created an inherited User Control named DateTimePicker2, inheriting from DateTimePicker that implements this event.因此,在我之前的答案的启发下,我创建了一个名为 DateTimePicker2 的继承用户控件,继承自实现此事件的 DateTimePicker。 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.并且不要忘记订阅设计师的 DateTimePicker2_ValueChanged 事件。

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我使用新的 Checked 属性(隐藏 base.Checked 属性)和 _checked 字段来保留旧值的原因是因为

  1. the base.Checked property does not fire the ValueChanged event when changed programmatically and therefore needed a new property that could do that. base.Checked 属性在以编程方式更改时不会触发 ValueChanged 事件,因此需要一个可以执行此操作的新属性。
  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. this.Checked 新属性在从 UI 更改时不会触发 ValueChanged 事件,因此需要一个可以跟踪 base.Checked 属性的标志。

Basically a combination of both approaches was needed.基本上需要两种方法的组合。

I hope this helps.我希望这有帮助。

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:您必须存储旧的“已检查”值才能与新值进行比较,因此您将能够确定“已检查”state 是否已更改:

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 ...

    }
}

I know this is super old but this could help someone.我知道这是超级旧的,但这可以帮助某人。

You can capture DataTimePicker.MouseUp event您可以捕获DataTimePicker.MouseUp事件

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.您需要对KeyUp事件执行相同的操作,才能获得同样可以激活复选框的 Space 按键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM