简体   繁体   English

如何从程序化文本框获取值?

[英]How to get values form programmatic text boxes?

I have code that programmatically add new labels and textboxes to a panel : 我有以编程方式向面板添加新标签和文本框的代码:

 Label newLabel;
 TextBox newTextBox;
 int txtBoxStartPosition = 75;
 int txtBoxStartPositionV = 25;

   for (int i = 0; i<LB.SelectedItems.Count; i++)
        {
            newLabel = new Label();
            newTextBox = new TextBox();
            newTextBox.Location = new System.Drawing.Point(
                                  txtBoxStartPosition + 150,
                                  txtBoxStartPositionV);
            newTextBox.Size = new System.Drawing.Size(70, 40);
            newLabel.Location = new System.Drawing.Point(
                                txtBoxStartPosition, 
                                txtBoxStartPositionV);
            newLabel.Size = new System.Drawing.Size(120, 40);
            newTextBox.Text = "0";
            newLabel.Text = LB.SelectedItems[i].ToString();
            this.panel1.Controls.Add(newTextBox);
            this.panel1.Controls.Add(newLabel);
            txtBoxStartPositionV += 50;
        }   

After run ... the user will enter values in the textboxes and he will click on a "ok" button. 运行后,用户将在文本框中输入值,然后单击“确定”按钮。 How can I get these values in : void button1_Click(object sender, EventArgs e) function ???? 如何在以下位置获取这些值: void button1_Click(object sender, EventArgs e)函数?

Since you are adding all your TextBox 's to panel1 you can access them like: 由于您将所有TextBox添加到panel1您可以像这样访问它们:

var allTextBoxesInPanel1 = panel1.Controls.OfType<TextBox>();

Then you can iterate the result and get value for each TextBox . 然后,您可以迭代结果并获取每个TextBox值。

foreach(TextBox textBox in allTextBoxesInPanel1)
{
    Console.WriteLine(textBox.Text);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取Gridview Row值的文本框? - How to get text boxes for Gridview Row values? 当我从多个文本框中插入单个值时,如何将值返回到多个文本框中 - How to get values back to multiple text boxes as i inserted from multiple text boxes to single value 如何在asp.net中获取与文本框名称相同的值? - how to get values of same name of text boxes in asp.net? 如何从动态生成的文本框中获取值? - how to get values from dynamically generated text boxes? 无法从表单上的文本框中获取值 - Unable to grab values from text boxes on form 从数据库获取多个值,并在索引更改C#时显示在组合框和不同的文本框中 - get multiple values form the database and show in combo box and different text boxes when the index change c# 文本更改时如何从程序化文本框中获取文本? - How to get text from programmatic textbox when text changes? 如何提交表单,但我的文本框将其值保留在asp.net Razor页面中 - How do I submit a form, but my text boxes preserve their values in asp.net Razor pages 从文本框中获取特定值到Grid View? - Get specific values from text boxes into Grid View? 如何绑定列表<T>到表单上的相关文本框? - How can I bind a List<T> to relevant text boxes on a form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM