How can i show month selection calendar in my app

20,962

Solution 1

I'm not entirely sure if I understand what you're asking. If you want to show the month calendar without a picker control attached to it, you can use the MonthCalendar control.

It looks like this (note the absence of the textbox and drop-down arrow):

  MonthCalendar control


If you're wondering if there's a way to limit the content that is displayed on the control to only the months themselves, rather than the individuals days of the month, then no that's not possible. The control does not provide any built-in facility for this.

However, if you do want to use the picker control, you can limit the values it displays to only the name of the month. Do that by specifying a custom format string:

myDateTimePicker.Format = DateTimePickerFormat.Custom
myDateTimePicker.CustomFormat = "MMMM"

This results in something like the following (notice that the drop-down content is unchanged, but the information that is recorded in the picker control itself is limited to the name of the month):

  DateTimePicker with custom format specified

Solution 2

This is going to require a bit of pinvoke to send the messages to get the calendar to switch the view. Implement the DateTimePicker's DropDown event handler like this:

    private void dateTimePicker1_DropDown(object sender, EventArgs e) {
        IntPtr cal = SendMessage(dateTimePicker1.Handle, DTM_GETMONTHCAL, IntPtr.Zero, IntPtr.Zero);
        SendMessage(cal, MCM_SETCURRENTVIEW, IntPtr.Zero, (IntPtr)1);
    }

    // pinvoke:
    private const int DTM_GETMONTHCAL = 0x1000 + 8;
    private const int MCM_SETCURRENTVIEW = 0x1000 + 32;

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

The view switching is animated, it is going to be visible to the user. This won't work for Windows versions prior to Vista. Use an online translator if necessary to translate this C# code to VB.NET

Share:
20,962
KoolKabin
Author by

KoolKabin

A freelance web developer Websites: 1.) http://biotechx.com 2.) http://highcountrytrekking.com 3.) http://firante.com 4.) http://himalayanencounters.com 5.) http://ajisai.edu.np 6.) http://environmentnepal.info/test 7.) http://treknepal.au 8.) http://sunshinetrekking.com 9.) http://taverntrove.com 10.) http://trekkingandtoursnepal.com 11.) http://outsourcingnepal.com

Updated on June 28, 2020

Comments

  • KoolKabin
    KoolKabin almost 4 years

    I am interested in showing list of 12 months like in similar way to datepicker's month selection control. But i don't like to show the date picker to show the dates of that month too... only month view is ok so that i can select month from the list.

    my desired output: enter image description here