DateTimePicker control in ContextMenuStrip

461 Views Asked by At

I have added a DateTimePicker in ContextMenuStrip using ToolStripControlHost. I am using below code.

 DateTimePicker startDate = new DateTimePicker();
 (cms.Items["mnuStartDate"] as ToolStripMenuItem).DropDownItems.Add(new ToolStripControlHost(startDate));

Selecting of date is working but when I clicked on the arrows to go to the previous or next month, or if I clicked on the Month, the ToolStrip is closing. Is there any workaround here? Thanks.

Note: I am showing the cms(ContextMenuStrip ) when Right Clicking on a control.

2

There are 2 best solutions below

0
On

Looks like the ToolStrip behavior closes when an item is selected. So I juts used CalendarMonth Control as a replacement. Link here

0
On

It seems to be related to Application.EnableVisualStyles(); When not-enabled, the menu doesn't unexpectedly close.

When enabled, to prevent closing, detect if the CalendarMonth menu is currently visible, and if the mouse was clicked on it.

public class DatePicker : DateTimePicker {

    private const int DtmFirst = 0x1000;
    private const int DtmGetmonthcal = (DtmFirst + 8);
    private bool isDroppedDown = false;
    private IntPtr hwndCalendarWindow = IntPtr.Zero;

    protected override void OnDropDown(EventArgs e) {
        isDroppedDown = true;
        hwndCalendarWindow = SendMessage(Handle, DtmGetmonthcal, 0, 0);
        base.OnDropDown(e);
    }

    protected override void OnCloseUp(EventArgs eventargs) {
        isDroppedDown = false;
        base.OnCloseUp(eventargs);
    }

    public bool CalendarMonthClicked(Point pt) {
        if (!isDroppedDown)
            return false;

        RECT r = new RECT();
        GetWindowRect(hwndCalendarWindow, out r);
        return pt.X >= r.Left && pt.X <= r.Right && pt.Y >= r.Top && pt.Y <= r.Bottom;
    }

    [DllImport("user32.dll")]
    private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

    [DllImport("User32.dll")]
    private static extern IntPtr SendMessage(IntPtr h, int msg, int param, int data);

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT {
        public int Left, Top, Right, Bottom;
    }
}

static class Program {

    //[DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
    //public extern static Int32 SetWindowTheme(IntPtr hWnd, String textSubAppName, String textSubIdList);

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles(); // if you disable visual styles then the menu won't close
        DatePicker dpStart = new DatePicker();
        dpStart.MinimumSize = new Size(100, 50);

        Form ff = new Form();
        ContextMenuStrip cms = new ContextMenuStrip();
        ToolStripMenuItem menu = new ToolStripMenuItem("Menu");
        menu.DropDown.Closing += (o, e) => {
            e.Cancel = (e.CloseReason == ToolStripDropDownCloseReason.AppClicked && dpStart.CalendarMonthClicked(Cursor.Position));
        };

        // sub-menu doesn't close properly, probably related to the ToolStripControlHost
        cms.Opening += delegate {
            menu.DropDown.Close(ToolStripDropDownCloseReason.CloseCalled);
        };

        cms.Closed += delegate {
            menu.DropDown.Close(ToolStripDropDownCloseReason.CloseCalled);
        };

        ff.ContextMenuStrip = cms;
        cms.Items.Add(menu);
        ToolStripControlHost item = new ToolStripControlHost(dpStart) { AutoSize = true };
        menu.DropDownItems.Add(item);
        Application.Run(ff);
    }
}