【问题标题】:C# How to set control height in relative heightC#如何在相对高度中设置控件高度
【发布时间】:2011-02-16 08:45:45
【问题描述】:

我的表单包含一个选项卡控件。在一个选项卡上,我有三个组框。每个分组框的高度必须是选项卡高度的三分之一。

现在我在选项卡控件的布局事件上有这段代码:

        // 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();

我的表单的构造函数中也有这段代码:

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

在您看来,设置组框高度的最佳方式是什么?当窗体调整大小时,组框高度必须是面板高度的三分之一。

我还希望尽可能减少屏幕上的闪烁。

【问题讨论】:

  • 它们似乎总是保持相同的相对大小,那么为什么不简单地在设计器中设置 Anchor 和/或 Dock 属性呢?您也可以使用 FlowLayoutPanel。

标签: c#


【解决方案1】:

您可以为此使用TableLayoutPanel。在面板中,您可以指定RowStyle 使其大小为父控件的百分比:

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));

只需将组框上的DockStyle 设置为DockStyle.Fill 并将它们添加到表格布局面板中。您可以使用设计器来执行此操作。

【讨论】:

  • Thnx,这可行,但是在调整窗口大小时,它仍然闪烁很多。如何防止屏幕闪烁?
  • 尝试使用 BeginUpdate() 和 EndUpdate()
  • 我应该在哪里使用它?我现在使用 TableLayoutPanel 并让控件现在处理高度。我应该将 Begin- 和 EndUpdate 方法放在哪里?
【解决方案2】:

使用分组框的 Achor 属性。

【讨论】:

  • 如何设置每个分组框的锚点设置?我的意思是哪个 groupbox 确实有哪个锚设置
  • 我认为组框应该设置为右上角的锚设置,如果它们一个接一个地放在下面。毕竟这取决于你的屏幕
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
  • 2011-09-21
相关资源
最近更新 更多