【问题标题】:getting text from dynamiclly created texbox [duplicate]从动态创建的文本框中获取文本[重复]
【发布时间】:2017-01-12 07:13:36
【问题描述】:

我正在尝试制作 Windows 应用程序来计算 GPA 。用户可以输入主题的数量,应用程序会创建与主题数量一样多的文本框。但我无法从文本框中获取值。这是我正在尝试的代码

private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < int.Parse(textBox1.Text); i++)
        {
            Label supjects = new Label();
            supjects.Text = "supject " + (i+1) ;
            supjects.Location = new System.Drawing.Point(60, 70+i*41);
            supjects.Name = "supject" + i;
            this.Controls.Add(supjects);

            TextBox fullMark = new TextBox();
            fullMark.Location = new System.Drawing.Point(162, 70 + i * 41);
            fullMark.Size=new System.Drawing.Size(57, 24);
            fullMark.Name = "fullMark" + i;
            this.Controls.Add(fullMark);

            TextBox yourMark = new TextBox();
            yourMark.Location = new System.Drawing.Point(272, 70 + i * 41);
            yourMark.Size = new System.Drawing.Size(57, 24);
            yourMark.Name = "yourMark" + i;
            this.Controls.Add(yourMark);


        }

【问题讨论】:

  • 谢谢,但你能把代码发给我吗? @L.B

标签: c# winforms dynamic textbox


【解决方案1】:

声明一个像

这样的字典
Dictionary<string, TextBox> textBoxes = new Dictionary<string, TextBox>();

并将循环中的文本框添加到其中。

 textBoxes.Add("yourMark" + i, yourMark);

现在你可以像这样使用它了

var text = textBoxes["yourMark2"].Text

【讨论】:

  • 你能在这一行中清除 (yourMark) 是什么吗 textBoxes.Add("yourMark" + i, yourMark);
【解决方案2】:
for (int i = 0; i < int.Parse(textBox1.Text); i++)
    {
        Label l = this.Controls.Find("supject" + i,true)[0] as Label;
        l.Text = "yes";
        //whatever
     }

要找到 YourMark,它是 标签 l = this.Controls.Find("YourMark",true)[0] as Label;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多