'TextBox'不包含'ID'的定义

Geh*_*man 0 c# winforms

我有以下代码在每个按钮单击上创建一个文本框:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

txt.ID = timerCount++;给我'TextBox' does not contain a definition for 'ID',虽然在回答说明这个问题,在这个页面.

dan*_*ish 5

您正在混合ASP.Net和Windows窗体.在ASP.Net中你有ID.在Windows窗体中,您可以通过Name属性访问控件.

更新:

您的代码实际上是创建多个文本框但位置相同.每次Y坐标都会重置.您可能想要成为boxY私人会员.

  • 不,它们是两种不同的技术.ASP.Net用于制作基于Web的应用程序,而Windows窗体用于创建桌面应用程序(您安装的东西) (2认同)