我有以下代码可在每次单击按钮时创建一个文本框:
private void AddNewTimer_Click(object sender, EventArgs e)
{
int timerCount = 0;
int boxY = 0;
if (timerCount <= 6)
{
timerCount++;
TextBox txt = new TextBox();
txt.ID = "timer" + timerCount++;
txt.Text = Convert.ToString(timerCount);
txt.Name = Convert.ToString(timerCount);
txt.Width = 63;
txt.Location = new Point(0, boxY);
TimerContainer.Controls.Add(txt);
boxY = boxY + 25;
}
}
txt.ID = timerCount++;
给了我'TextBox' does not contain a definition for 'ID'
,尽管它是在回答this问题和在this页面上描述的。
答案 0 :(得分:5)
您正在混合使用ASP.Net和Windows窗体。在ASP.Net中,您有ID
。在Windows窗体中,您可以通过Name
属性访问控件。
更新:
您的代码实际上正在创建多个文本框,但是位置相同。您的Y坐标每次都会重置。您可能想将boxY
设为私人会员。