简体   繁体   中英

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 .

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM