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 out?
A DateTimePicker control allows users to select a date and time in Windows Forms applications.
The Windows Forms DateTimePicker control allows the user to select a single item from a list of dates or times. When used to represent a date, it appears in two parts: a drop-down list with a date represented in text, and a grid that appears when you click on the down-arrow next to the list.
The DateTimePicker.Value has the type of System.DateTime. The MSDN says: Remarks. If the Value property has not been changed in code or by the user, it is set to the current date and time (DateTime. Now).
It does however trigger the value changed event
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 ...
}
}
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
Basically a combination of both approaches was needed.
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With