Max
Max

Reputation: 3593

Handle click on Today in a DateTimePicker

I would like to handle the user click on the Today part of a DateTimePicker. I looked into the decompiled source code of the System.Windows.Forms.DateTimePicker looking for an event or method to override but could not find any.

I know I could handle the ValueChanged event and compare it to DateTime.Today but I specifically need to handle the click on the Today button at the bottom (see screenshot).

DateTimePicker

Is there a way to do that by using the standard control (or by inheriting it) or should I use/create a full custom control for that?

Upvotes: 2

Views: 960

Answers (1)

Dmitry
Dmitry

Reputation: 14059

If you could calculate the rectangle of the today's row:

Rectangle todayRect = new Rectangle(dateTimePicker1.Left, dateTimePicker1.Bottom + 160, dateTimePicker1.Width, 20);

then it's possible to compare the current mouse position to that rectangle in the CloseUp event handler:

dateTimePicker1.CloseUp += (s, e) =>
   {
       Point p = Cursor.Position;
       if (todayRect.Contains(PointToClient(p)))
           Console.WriteLine("Today!");
   };

Upvotes: 1

Related Questions