You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中动态将文本框整数值存入int数组并生成表单控件

WinForm动态生成控件并存储数值需求实现进度

我正在做一个WinForm项目,想要实现这样的功能:表单里有个按钮,点击它就能动态生成一组控件——包含2个文本框和1个标签,还要把文本框中输入的整数值存储到int数组里。目前已经完成了部分代码实现,具体内容如下:

按钮点击事件代码

private void button1_Click(object sender, EventArgs e) 
{ 
    AddNewLabel(); 
    AddNewTextBox1(); 
    AddNewTextBox2(); 
}

添加文本框的代码

public System.Windows.Forms.TextBox AddNewTextBox1() 
{ 
    System.Windows.Forms.TextBox txt1 = new System.Windows.Forms.TextBox(); 
    this.Controls.Add(txt1); 
    txt1.Top = txtBoxCounter * 28; 
    txt1.Left = 125; 
    string txtName1 = "txtLL" + txtBoxCounter; 
    txt1.Name = txtName1; 
    string txtValue1 = txt1.Text; 
    lowerLimit[txtBoxCounter-1] = Int32.Parse(txtValue1); 
    return (txt1); 
}

添加标签的代码

public System.Windows.Forms.Label AddNewLabel() 
{ 
    System.Windows.Forms.Label lbl = new System.Windows.Forms.Label(); 
    this.Controls.Add(lbl); 
    lbl.Top = lblCounter * 28; 
    lbl.Left = 15; 
    lbl.Text = "Range" + lblCounter; 
    lblCounter = lblCounter + 1; 
    return (lbl); 
}

效果展示

  • 初始表单:
    初始表单

  • 点击按钮后的表单:
    点击按钮后的表单

内容的提问来源于stack exchange,提问作者Khandkar Asif Hossain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:22:26