2012-02-23 67 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

相关问题