Question on DateTimePicker

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • priyamtheone
    New Member
    • Sep 2007
    • 88

    Question on DateTimePicker

    In C# 2005 how can i trap the checked/unchecked state of a datetimepicker through an event. I'm trying to execute two different methods based on the checked/unchecked state of a datetimepicker when the user clicks the datetimepicker. When the user checks the dtPicker Method A is executed, when the user unchecks it Method B is executed. How can i trap it.
    Regards.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I was expecting to give an obvious answer like "Use the CheckChanged event". After all one would expect that if it has a checkbox and a Checked property in it that it would have a CheckChanged event, right? Nope. Thanks for the oversight Microsoft.

    So then I figured, 'well it has to raise some event: The font color is changing as if it is enabled/disabled.' So I put in simple methods for various events like EnableChanged, FontChanged and so on. Nope.

    The only thing I could come up with was reacting to the MouseUp event.
    Code:
            private void dateTimePicker1_MouseUp(object sender, MouseEventArgs e)
            {
                if (((DateTimePicker)sender).Checked)
                {
                    Console.WriteLine("Is checked");
                }
                else
                {
                    Console.WriteLine("NOT checked");
                }
            }
    You could add a bool for "WasChecked ", then compare the current status to the previous status. If they don't match, then there was a change. If they do match, then the user clicked some other area of the DTP control.

    If you are going to use this control often and want that as a true event, you might want to make your own DTP control inherited from this one where you raise an event for the CheckChanged.

    If anyone else knows a more elegant solution, let's here it.

    Comment

    Working...