c# winform动态生成控件问题

单一生成控件我 知道,
TextBox ttt = new TextBox();
ttt.Name = "TextBox1";
this.Controls.Add(ttt);
ttt.Location = new Point(130, 10);
但是我不止添加一个控件,例如我点击一个按钮,添加第一个textbox,再点击,在第二个位置添加第二个textbox等等,当第一行的textbox到达窗体的最大宽度,则马上从第二行开始添加,第二行添加满了,第三行继续,依次循环。

这个还不简单吧,你要判断一下容器宽度及当前一个控件如果放置后的LEFT+WIDTH是不是在你的容器内,如果不在你就要换行(TOP+N)追问

有代码么,现在两个问题,怎么点击的时候自动多添加一个textbox,第二个问题,就是怎么判断当前行到了窗体的最宽,到了就换行。关键是不会代码啊,求代码。

追答

int fullwidth= this.Width //form width
int textwidth=100;
int textSpan=20;
int currentCol=0;
int height=40;
int currentRow=0;
button_click event code
currentCol++;

if(currentCol*(textwidth+txtSpan)<fullwidth)
{
TextBox ttt = new TextBox();
ttt.Name = "TextBox"+currentrow+"-"+;currentCol this.Controls.Add(ttt);
ttt.Location = new Point(cucurrentCol*(textwidth+txtSpan),height*currentrow);
}
else
{
curentcol=0;
currentrow++
}

温馨提示:答案为网友推荐,仅供参考
相似回答