How can i show month selection calendar in my app
Asked Answered
E

4

5

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

Elise answered 11/3, 2011 at 7:19 Comment(0)
F
4

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

Fungistat answered 11/3, 2011 at 8:40 Comment(4)
sorry i mean i need the picker too but i only need user to select month not date... like users should be able to select jan, feb not jan 1, jan 15Elise
@KoolKabin: Make sure you've refreshed the page to see the second part of my answer. If all you care about is user input, I think that will work for you just fine. They'll be able to see individual dates, but they won't be able to select them (or at least, their selection will be ignored by your app).Fungistat
To avoid the display of the dropdown and to display ONLY month and/or year you can set to True the property ShowUpDownDandruff
What are the property values that are required to display the MonthCalender like it is seen in your first image? I want to display a calendar to only show months/year but I cannot get it to appear like your first example image.Spleeny
S
7

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

Sacramentalism answered 11/3, 2011 at 9:20 Comment(5)
Interesting, I didn't know the view could even be set like that. But I don't suppose this would prevent the user from changing the view himself once the control is visible, either.Fungistat
@Cody, nope, just clicking a month will make it switch views. Such a click isn't detectable, only the view change is. Not so sure if the OP realizes this. Oh well, he didn't ask for it.Sacramentalism
@HansPassant I tried it on a Windows Forms application (C#, Windows 10), the suggested code does not limit the dropped calendar view to months only. Date are visible and selectableTreulich
@HansPassant, Is possible to show yearly only? I'm created daily, monthly and yearly reports and need that view.Mocha
@HansPassant Last paramter of SendMessage: (IntPtr)1 for monthly and (IntPtr)2 for yearly. ThanksMocha
F
4

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

Fungistat answered 11/3, 2011 at 8:40 Comment(4)
sorry i mean i need the picker too but i only need user to select month not date... like users should be able to select jan, feb not jan 1, jan 15Elise
@KoolKabin: Make sure you've refreshed the page to see the second part of my answer. If all you care about is user input, I think that will work for you just fine. They'll be able to see individual dates, but they won't be able to select them (or at least, their selection will be ignored by your app).Fungistat
To avoid the display of the dropdown and to display ONLY month and/or year you can set to True the property ShowUpDownDandruff
What are the property values that are required to display the MonthCalender like it is seen in your first image? I want to display a calendar to only show months/year but I cannot get it to appear like your first example image.Spleeny
J
0

Another way would be to set the date time picker Format property to custom and in the custom format property enter MMMM to show the name of the month. Then set the show up down to true so it will bypass the drop down and let the user go up and down through the months.

Jaggy answered 27/6, 2014 at 15:58 Comment(0)
A
-2

Put the MonthCalendar control into a GroupBox. Shrink the GroupBox so that only the Month part of the MonthCalendar control is visible to the form. This will hide the days.

Adler answered 30/1, 2014 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.