Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonthCalendar width on different (platforms), correction: themes (XP vs Aero theme)

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 :

enter image description here

How to compensate for that ?

like image 528
Run CMD Avatar asked May 24 '11 10:05

Run CMD


2 Answers

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.

like image 200
Teoman Soygul Avatar answered Oct 26 '22 17:10

Teoman Soygul


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:

Form with MonthCalender control

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 AutoSizeand 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:

Form's properties (Visual Studio)

Now run the program and you will see that the size of the form changes automatically depending of the size of the MonthCalender control:

Result

That's it! (remember to switch your Region format setting back to the original one)

;-) Dave

like image 37
Sigja Avatar answered Oct 26 '22 17:10

Sigja