Capture doubleclick to control MonthCalendar in windows forms application

How to capture the doubleclick event of a MonthCalendar control? I tried using the MouseDown property MouseEventArgs.Clicks, but always 1, even if I double-clicked.

+4
source share
3 answers

Note that MonthCalendar does not show the DoubleClick or MouseDoubleClick event in the Property window. A sure sign of a problem, the native Windows control prevents these events from being received. You can synthesize your own by observing MouseDown events and measuring the time between clicks.

Add a new class to your project and paste the code shown below. Compilation. Drop the new control on top of the toolbar. Write an event handler for the DoubleClickEx event.

using System; using System.Windows.Forms; class MyCalendar : MonthCalendar { public event EventHandler DoubleClickEx; public MyCalender() { lastClickTick = Environment.TickCount - SystemInformation.DoubleClickTime; } protected override void OnMouseDown(MouseEventArgs e) { int tick = Environment.TickCount; if (tick - lastClickTick <= SystemInformation.DoubleClickTime) { EventHandler handler = DoubleClickEx; if (handler != null) handler(this, EventArgs.Empty); } else { base.OnMouseDown(e); lastClickTick = tick; } } private int lastClickTick; } 
+4
source

You will need to follow click events yourself. You need to use the DateSelected event to mark when you click on a date, and the DateChanged event is "reset", so you do not think that clicking on different dates is like a double click.

Note. If you use the mouse down event, you will get bad behavior

The mouse down event occurs regardless of what is clicked, for example, a click on the heading of a month / year, etc. in the calendar will be registered in the same way as pressing a real date. Therefore, using DateSelected instead of a mouse event.

 private DateTime last_mouse_down = DateTime.Now; private void monthCalendar_main_DateSelected(object sender, DateRangeEventArgs e) { if ((DateTime.Now - last_mouse_down).TotalMilliseconds <= SystemInformation.DoubleClickTime) { // respond to double click } last_mouse_down = DateTime.Now; } private void monthCalendar_main_DateChanged(object sender, DateRangeEventArgs e) { last_mouse_down = DateTime.Now.Subtract(new TimeSpan(1, 0, 0)); } 
+2
source

It is best to add the following code, otherwise, if you quickly click on two dates, you will have an event.

  protected override void OnDateChanged(DateRangeEventArgs drevent) { lastClickTick = Environment.TickCount - 2 * SystemInformation.DoubleClickTime; base.OnDateChanged(drevent); } 
0
source

Source: https://habr.com/ru/post/1386209/


All Articles