[英]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.