Martijn
Martijn

Reputation: 24789

C# How to set control height in relative height

My form contains a tab control. On one tab, I have three groupboxes. Each groupbox must have the height of one third of the height of the tab.

Right now I have this code on the layout event of the tab control:

        // Determine the position of the group panels (gp)
        this.SuspendLayout();
        int oneThird = (tabPanel.Height - 5) / 3;

        if (_currentQuestion != null && _currentQuestion.QuestionTypes == QuestionTypes.Infofield)
        {
            gpExplanation.Visible = false;
            gpFillInHelp.Visible = false;
            gpFillInQuestion.Height = oneThird * 3;
        }
        else
        {
            gpExplanation.Visible = true;
            gpFillInHelp.Visible = true;

            gpFillInQuestion.Height = oneThird;
            gpExplanation.Height = oneThird;
            gpFillInHelp.Height = oneThird;
        }

        // Determine position of the appointment and achievement group panels
        int height = tabPanel.Height - 30;
        int half = height / 2;

        pnlAppointment.Height = half;

        this.ResumeLayout();

I also have this code in the constructor of my form:

// Set styles which prevent screen flickering
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
this.DoubleBuffered = true;

What is in your eyes the best way to set the height of the groupboxes? When the form resizes, the groupboxes height must be one third of the panels height.

I also want as less as possible flickering on the screen.

Upvotes: 2

Views: 2844

Answers (2)

Simon Fischer
Simon Fischer

Reputation: 3876

You can use a TableLayoutPanel for this. In the panel you can specify the RowStyle such that it sized as a percentage of the parent control:

tableLayoutPanelGroupBoxes.ColumnCount = 1;
tableLayoutPanelGroupBoxes.RowCount = 3;
tableLayoutPanelGroupBoxes.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33333F));
tableLayoutPanelGroupBoxes.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33334F));
tableLayoutPanelGroupBoxes.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33334F));

Just set the DockStyle on your group boxes to DockStyle.Fill and add them to the table layout panel. You can use the designer to do this.

Upvotes: 3

Nirmal
Nirmal

Reputation: 1245

Use Achor property of the group boxes.

Upvotes: 0

Related Questions