[英]How to create textbox at run time and take values from these text box ? in C# . Net
I need your help in making text box at run time and taking values from these text boxes that user enter. 在运行时创建文本框并从用户输入的这些文本框中获取值时,我需要您的帮助。 i have two button and one rich_text_box , when user click on one button it creates 3 text boxes and then user click on other button it should take value from text boxes and how in rich text box .
我有两个按钮和一个rich_text_box,当用户单击一个按钮时,它将创建3个文本框,然后用户单击其他按钮,它应从文本框中获取值以及如何在富文本框中显示值。
this is code i am using to create dynamic textbox 这是我用来创建动态文本框的代码
private void create_textbox_Click(object sender, EventArgs e)
{
flowLayoutPanel1.Controls.Clear();
for(i=1;i<=3;i++)
{
TextBox text = new TextBox();
text.Name = "Text Box" + i.ToString();
//text.Text = "Text Box " + i.ToString();
flowLayoutPanel1.Controls.Add(text);
}
}
and this code i am using to take values from new created text boxes and displaying in rich text box . 我正在使用此代码从新创建的文本框中获取值并在富文本框中显示。
private void get_value_Click(object sender, EventArgs e)
{
TextBox text = new TextBox();
for (i = 1; i <= 3; i++)
{
string value = text.Text + i.ToString();
richTextBox1.SelectedText = "\r\n" + value;
}
}
This should solve your problem: 这应该可以解决您的问题:
private void get_value_Click(object sender, EventArgs e)
{
for (var c in flowLayoutPanel1.Controls)
{
var t = c as TextBox;
if (t != null)
{
richTextBox1.SelectedText = "\r\n" + t.Text;
}
}
}
In your method get_value_Click you aren't using any of the text boxes that were added to the flow layout panel. 在您的方法get_value_Click中,您没有使用添加到流布局面板中的任何文本框。 Something similar to Wolfgang Ziegler's answer should work but you will need to check the Type and Name in case you have other controls in the flow layout panel.
与Wolfgang Ziegler的答案类似的方法应该起作用,但是如果您在流布局面板中还有其他控件,则需要检查“类型”和“名称”。
private void get_value_Click(object sender, EventArgs e)
{
for (i = 1; i <= 3; i++)
{
string value = this.flowLayoutPanel1.Controls["Text Box" + i].Text;
richTextBox1.SelectedText = "\r\n" + value;
}
}
This ought to do it. 这应该做到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.