2011-02-16 47 views
2

我的表單包含一個選項卡控件。在一個標籤上,我有三個組合框。每個組框的高度必須爲標籤高度的三分之一。C#如何在相對高度設置控件高度

現在我對標籤控件的佈局事件驗證碼:

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

什麼是你的眼睛設置groupboxes高度的最佳方法是什麼?當窗體大小調整時,組框高度必須是面板高度的三分之一。

我也希望儘可能少閃爍在屏幕上。

+0

看起來好像它們總是保持相同的相對大小,那麼爲什麼不簡單地在設計器中設置錨和/或Dock屬性?你也可以使用FlowLayoutPanel。 – 2011-02-16 08:48:18

回答

3

您可以使用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)); 

只需設置你的組合框的DockStyleDockStyle.Fill並將它們添加到表格的佈局面板。你可以使用設計器來做到這一點。

0

使用組框的Achor屬性。

+0

如何設置每個組框的錨定設置?我的意思是哪個組合框具有哪個錨定設置 – Martijn 2011-02-16 09:08:48

+0

我認爲組框應該使用右上角錨定設置進行設置,如果它們放在一個接一個下面的話。畢竟這取決於你的屏幕 – Nirmal 2011-02-16 09:46:24