Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get datetimepicker c# winform checked/unchecked event

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?

like image 764
Thunder Avatar asked Aug 17 '11 08:08

Thunder


People also ask

What is DateTimePicker C#?

A DateTimePicker control allows users to select a date and time in Windows Forms applications.

How do I use DateTimePicker in Windows form?

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.

What does DateTimePicker return?

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


3 Answers

It does however trigger the value changed event

like image 130
BugFinder Avatar answered Sep 18 '22 17:09

BugFinder


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

    }
}
like image 44
Mr_CRivera Avatar answered Sep 18 '22 17:09

Mr_CRivera


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.

like image 30
GDS Avatar answered Sep 21 '22 17:09

GDS