2013-04-22 68 views
0

我正在使用vs2012(C#)爲我的應用程序,我正在尋找一種方法將標籤和文本框添加到我的窗體上的tabPage中。但是由於要添加的控件數量可能大於10,我還試圖將它們添加到「列」中,以便容器控件只能水平滾動,而不是垂直滾動。根據容器高度動態添加控件的「列」

舉例來說,我試圖做這樣的事情:

LabelControl  LabelControl  LabelControl  LabelControl 
TextboxControl TextboxControl TextboxControl TextboxControl 

LabelControl  LabelControl  LabelControl  LabelControl 
TextboxControl TextboxControl TextboxControl TextboxControl 

etc. 

「容器」控制是一個TabPage的,所以我知道我必須抓住從高度和使用它。我能夠獲取文本框進行渲染,但是在頂部的標籤控件,以及下面的文本框中遇到困難。

這裏是我到目前爲止有:

int height = tabPageBicycle.Height; 
Point startLocation = new Point(0, 0); 
int previousX = 0; 
int previousY = 0; 
int currentX = 0; 

for (int x = 0; x < 75; x++) 
{ 
    Label label = new Label(); 
    TextEdit text = new TextEdit(); 
    label.Text = x.ToString(); 
    text.Text = x.ToString(); 

    label.Location = new Point(currentX, previousY); 
    tabPageBicycle.Controls.Add(label); 

    if ((height - previousY) < text.Height) 
    { 
     currentX += 100; 
     previousY = 0; 
    } 

    text.Location = new Point(currentX + text.Height + 5, previousY + 50); 
    previousX = text.Location.X; 
    previousY = text.Location.Y; 
    tabPageBicycle.Controls.Add(text); 
} 

任何線索,我在做什麼錯?

回答

0

完成了它,逐行採取並看到循環中正在做什麼。這是我使用的最終代碼....一如既往,我願意接受建議/評論/等。如何使其更好/更高效。

int labelY = 0; 
int textY = 0; 
int startX = 5; 
int startY = 5; 
int height = tabPageBicycle.Height; 
Point startLocation = new Point(0, 0); 

for (int x = 0; x < 75; x++) 
{ 
    Label label = new Label(); 
    TextEdit text = new TextEdit(); 
    label.AutoSize = true; 
    label.Text = x.ToString(); 
    text.Text = x.ToString(); 

    //Determine if the next set of controls will be past the height of the container. 
    //If so, start on the next column (change X). 
    if ((height - textY) < ((label.Height + 10) + text.Height)) 
    { 
     startX += 125; 
     labelY = 0; 
    } 

    //Start of new column. 
    if (labelY == 0) 
     label.Location = new Point(startX, startY); 
    else 
     label.Location = new Point(startX, textY + label.Height + 10); 

    tabPageBicycle.Controls.Add(label); 
    labelY = label.Location.Y; 
    textY = labelY + 15; 

    text.Location = new Point(startX, textY); 
    textY = text.Location.Y; 
    tabPageBicycle.Controls.Add(text); 
} 

,結果: enter image description here

我希望它可以幫助別人了!

相關問題