Apparently the .NET monthcalendar renders differently on different platforms. A calendar on Vista is wider than a XP calendar.
I want to make the calendar fit nicely and precise on all platforms. Is there a way to do this, without having to measure and hard code the different widths ?
..............
Edit/Correction : The calendar seems to render differently based on the theme you select :
How to compensate for that ?
That is the expected behavior. If you don't want to be effected by themes, disable theming completely (i.e. don't execute the command Application.EnableVisualStyles();
). Otherwise different themes will always yield different looks for the controls (as they are meant to do). If you want to fit the controls in at all times, use a more fluent layout making use of anchors and docking.
I had the same problem and found a workaround:
It seems that the dimensions of the MonthCalendar
control are updated correctly when it is shown (like in a form) in runtime.
Use, e.g., the form's Shown
event to know when the dimensions are updated.
You can also set the form's AutoSize
property to true
and AutoSizeMode
to GrowAndShrink
to make the form automatically fit the MonthCalendar
control.
Update:
For more details try this example:
Put a MonthCalendar control on a form like this:
In the form's Shown event add this:
public static int CalenderWidth = 0, CalenderHeight = 0;
private void Form1_Shown(object sender, EventArgs e)
{
CalenderWidth = monthCalendar1.Width;
CalenderHeight = monthCalendar1.Height;
MessageBox.Show("MonthControl width: " + CalenderWidth.ToString() +
", height: " + CalenderHeight.ToString());
}
When the program is run you will see a messagebox showing the right dimensions. The Width and Height are also put into two variables you can use anywhere in your program (in a quick and dirty way, I know ;-) Of course you can omit the messagebox if you don't want it.
To check it is really working try changing the Region settings in Windows: Change the Format to e.g. Danish. Run the program again and you will see the Width has gotten smaller because a Danish MonthCalender control is smaller.
Regarding the AutoSize
and AutoSizeMode
properties they can be used to make the size of the form adapt to the size of the MonthCalender control. Do this:
Change the two properties in the form into this:
Now run the program and you will see that the size of the form changes automatically depending of the size of the MonthCalender control:
That's it! (remember to switch your Region format setting back to the original one)
;-) Dave
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With