2012-02-23 56 views
1

我需要在我的WinForm應用程序的表單上放置「x」個標籤。下面的功能已經做到了。我缺少的是能夠增加每個標籤之間的差距,而不增加標籤的尺寸。我知道這可能是簡單的,但對於我的生活,我似乎無法弄清楚。我提前爲這樣一個愚蠢的問題道歉。如何增加網格中動態添加的標籤之間的差距?

private void AddUserControl() 
{ 
    int ucHeight = 60; 
    int ucWidth = 320; 
    int spacer = 20; 
    int start_x = 10; 
    int start_y = 10; 
    int NumOfRows = 6; 
    int NumOfColumns = 3; 
    int totalProblems = 17; 
    int ucCounter = 0; 

    for (int x = 0; x < NumOfRows; x++) 
    { 
     for (int y = 0; y < NumOfColumns; y++) 
     { 
      if (ucCounter < totalProblems) 
      { 
       Label myLabel = new Label(); 
       myLabel.Top = start_x + (x * ucHeight + spacer); 
       myLabel.Left = start_y + (y * ucWidth + spacer); 
       myLabel.Width = ucWidth; 
       myLabel.Height = ucHeight; 
       this.Controls.Add(myLabel); 
       ucCounter++; 
      } 
     } 
    } 
} 
+0

你想說gap而不是gab? – om471987 2012-02-23 22:29:25

回答

1
myLabel.Left = start_y + (y * ucWidth + spacer); 

你的括號是在錯誤的地方。

var real_start_y = start_y + spacer; 
myLabel.Left = real_start_y + (y * ucWidth); 

你想要的是

myLabel.Left = start_y + y * (ucWidth + spacer); 

使每列增加了與前一個spacer分離:給它等同。

計算Top時出現同樣的問題。

+0

感謝您捕捉我的錯誤。我覺得自己像個白癡。 – TalShyar 2012-02-23 22:52:57

1

我認爲你可能尋找填充,但檢查出this link (MSDN),你應該能夠決定什麼最適合您的需求。

+0

對此感到抱歉,但這是在WinForm中,而不是WPF。我想,因爲我使用WinForm標籤,這將是明確的。 – TalShyar 2012-02-23 22:41:42

+0

它與WPF或WinForms無關 - 填充,邊距和佈局在這兩個地方以及Web開發都是相關的。我只是簡單地將這篇文章連接起來,因爲我認爲它很好地解釋了這些概念祝你好運! – 2012-02-23 22:43:26

+0

好的。謝謝。讓我更仔細地閱讀那篇文章。 – TalShyar 2012-02-23 22:44:17

相關問題