Kjartan
Kjartan

Reputation: 19111

Calculate size for a form containing a control that may vary in size?

I need to display a MonthCalendar, in a separate dialog, like the following:

enter image description here

The problem is that the display of this calendar depends on the system the application is running on, resulting in variations such as this (from the same application, on a different PC):

enter image description here

As I understand it, this depends on the visual style or theme the application happens to be running under. The question is, how can I calculate the appropriate size of the form the calendar needs to be placed in, or in some other way make this look elegant on several systems with different themes?

I'm far from satisfied with the current code, which places a calendar in a form (container), which is given a (hard-coded) size appropriate for one specific theme (the first pic):

private static Form SetUpCalendar()
{
    var calendar = new MonthCalendar
                {
                    MaxSelectionCount = maxSelectionCount,
                    BackColor = SystemColors.Info,
                    CalendarDimensions = new Size(1, 1),
                    // (..etc...)                    
                };

    var container = new Form
                {
                    FormBorderStyle = FormBorderStyle.FixedDialog,
                    Size = new Size(calendar.Width + 74, calendar.Height + 47)
                };

    container.Controls.AddRange(new Control[] {calendar});

    return container;
}

I've not been able to find a way to adjust the size of the form better to the calendar automatically, and would be grateful for any tips on how to improve this.

Upvotes: 0

Views: 904

Answers (1)

jac
jac

Reputation: 9726

Why not set the form's AutoSize property = true, and AutoSizeMode to GrowAndShrink? You also need to set the margin property if you want to eliminate the space around the control.

Upvotes: 4

Related Questions