ADO/ADO.NET - Retrieve text from Dyanmically added text box

Asked By Chiranjeevi S on 14-Sep-10 07:03 AM
Hi
 i want to retrieve the text box which is dynamically created by a button click

can any one help
Sasha Kotlo replied to Chiranjeevi S on 14-Sep-10 07:43 AM
Read this tutorial:

http://www.4guysfromrolla.com/articles/081402-1.aspx

I stress, it is a MUST read if you want to work with dynamic controls.

Regards.
Kirtan Patel replied to Chiranjeevi S on 14-Sep-10 07:52 AM
    private void button2_Click(object sender, EventArgs e)
    {
      /* creating Textbox By first Button*/
      TextBox t = new TextBox();
      t.Name = "txtDynamicTextBox";
      t.Text = "Hello World";
      t.Location = new Point(0, 0);
      this.Controls.Add(t);
    }

    private void button3_Click(object sender, EventArgs e)
    {
      
      /* Retriving Text From Dynamically Created TextBox */
      foreach (Control c in this.Controls)
      {
        if (c is TextBox)
        {
          if (c.Name == "txtDynamicTextBox")
          {
            MessageBox.Show("Text On Text Box ="+((TextBox)c).Text);
          }
        }

      }
    }
Super Man replied to Chiranjeevi S on 14-Sep-10 09:40 AM
If the Textbox is created and Not added in Form , then it will no more available when it goes out of scope.

if it is added to Form , then you can Get it using this.Controls .
Goniey N (Mr. G) replied to Chiranjeevi S on 14-Sep-10 11:45 PM
-- Use Below Simple Code :

01.using System.IO;
02.using System.Linq;
03.using System.Text;
04.using System.Windows.Forms;
05. 
06. 
07.namespace WindowsFormsApplication1
08.{
09.  public partial class Form1 : Form
10.  {
11.  //Decalre As Global For Use Anywhere...
12.    TextBox txtMyBox1 = new TextBox();
13. 
14.  private void button1_Click(object sender, EventArgs e)
15.  {
16.    //Set The Location On The Form Using X & Y Axes...
17.    txtMyBox1.Location = new Point(12, 12);
18. 
19.    //It Will Add New Textbox To Form Runtime...
20.    this.Controls.Add(txtMyBox1);
21.  }
22.   
23.  //Using Below You Can Retrieve value Of New Textbox...
24.  private void button2_Click(object sender, EventArgs e)
25.  {
26.     textBox1.Text = txtMyBox1.Text;
27.  }
28.}


-- For More Clear View See Below Images :


-- First View Of The Form...



-- After Click on "Add Textbox", It Will Add New Textbox...




-- Value Of New Textbox Will Display In Old Textbox Which Is Already In the Form....





-- It Will Work 100%...
-- Hope This Will Helpful To You...
Chiranjeevi S replied to Goniey N (Mr. G) on 15-Sep-10 01:18 AM
Aha!  Its Working Very well

Thankx
Chiranjeevi S replied to Goniey N (Mr. G) on 15-Sep-10 01:18 AM
Aha!  Its Working Very well

Thankx
Goniey N (Mr. G) replied to Chiranjeevi S on 16-Sep-10 12:14 AM
You Are Always Welcome....
Aaron replied to Goniey N (Mr. G) on 09-Dec-10 05:52 PM
What about a list of dynamically created controls? Like say you have:

List<TextBox> tbList = new List<TextBox>();

foreach(EnumerableObject obj in myCollection){
  TextBox t = new TextBox();
  t.Text = obj.SomeTextProperty;
}

Let's say we have a collection of six objects and the user changes the text in one of the text boxes, I want to write that new string to the corresponding object instance property.
Aaron replied to Aaron on 09-Dec-10 05:54 PM
oops sorry, I forgot tbList.Add(t);
this.Controls.Add(t);