如何在另一个以c sharp动态创建的方法中为文本框分配值

时间:2013-12-26 14:33:01

标签: c# winforms

如何在开始暂停按钮方法中为文本框分配值,该方法是在c sharp

中动态创建的
 public void btn_addtimer_Click(object sender, EventArgs e)
    {
        var panel1 = new Panel() 
            { 
                Size = new Size(500, 180), 
                Location = new Point(10, i), 
                BorderStyle = BorderStyle.FixedSingle 
            };
        TextBox textseconds = new TextBox();
        textseconds.Name = "txtseconds";
        textseconds.Location = new Point(350, 50);
        textseconds.KeyPress += textseconds_KeyPress;
        panel1.Controls.Add(textseconds);


        Button startpause = new Button();
        startpause.Name = "btnstartpause";
        startpause.Text = "Start";
        startpause.Location = new Point(350, 80);
        startpause.Click += btnstartpause_Click;
        panel1.Controls.Add(startpause);

    }

2 个答案:

答案 0 :(得分:3)

使用带有强制转换的FindControl:

((TextBox)FindControl("textseconds")).Text = "Some text here";

答案 1 :(得分:0)

为他们分配ID

        textseconds.Id ="txtbxScnds";

然后使用

((TextBox)FindControl("textseconds")).Text = "Some text here";

FindControl方法在当前命名容器中搜索具有指定id参数的服务器控件。

但是,您需要在每次回发时重新创建动态控件。