Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

getting information from dynamically added text boxes.

Posted on 2011-02-24
8
Medium Priority
?
191 Views
Last Modified: 2012-05-11
I am trying to save the information added to dynamic textboxes when I press the Submit Button. How do i refrence these boxes since they are not initially part of the class? Parts.docx
public partial class AddParts : Form
    {
        public AddParts( int x)
        {
            
            InitializeComponent();
            textBox1.Visible = false;
            textBox1.Text = Convert.ToString(x);
           
           
        }

        private void AddParts_Load(object sender, EventArgs e)
        {
            decimal y=0;
            int z=0;
            int xloc = 20;
            int yloc = 40;
            int lng = 100;
            int hgt = 20;
            
            int cntr = 0;
            string nme = "textBox"+ Convert.ToString(cntr+1);
            int x = Convert.ToInt32(textBox1.Text);


            do
            {

                // Add Lables:
                if (yloc == 40)
                {
                  Label  label = new System.Windows.Forms.Label();
                    label.AutoSize = true;
                    label.Location = new System.Drawing.Point(xloc, 15);
                    label.Name = "label" + Convert.ToString(cntr+1);
                    label.Size = new System.Drawing.Size(211, 13);
                    label.TabIndex = 99999+cntr;
                    
                    
                        if (cntr % 2 != 0)
                        {
                            label.Text = "Part Number:";
                        }
                        if (cntr % 2 == 0 && cntr !=0)
                        {
                            label.Text = "Part Description:";
                        }
                    Controls.Add(label);
                    label.Visible = true;
                    
                }
                // Add Text Boxes:
                TextBox textBox = new System.Windows.Forms.TextBox();
                textBox.Location = new System.Drawing.Point(xloc, yloc);
                textBox.Name = nme;
                textBox.Size = new System.Drawing.Size(lng, hgt);
                textBox.TabIndex = cntr+1;
                Controls.Add(textBox);
                textBox.Visible = true;
                cntr++;
                if (cntr % 2 == 0)
                {
                    xloc = xloc + 120;
                }

                else
                {
                    if (cntr == 1)
                    {
                    }
                    else
                    {
                        yloc = yloc + 25;
                        xloc = xloc - 120;
                    }
                }
                nme = "textBox" + Convert.ToString(cntr + 2);

                y = (Convert.ToDecimal(cntr) - 1) / 50;
                
                if (y!= 0 && int.TryParse(Convert.ToString(y), out z ))
                {
                    yloc = 40;
                    xloc = xloc + 260;
                }


            } while (cntr <= 2*x);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            
        }
    }

Open in new window

0
Comment
Question by:rciner313
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 34974779
You reference the textbox by name.

So you have TextBox textbox = new.....TextBox();

So to get the contents on your save event just call:

textbox.Text.ToString().Trim();

Open in new window

0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 34974787
You'd probably want to declare the TextBox textbox object *outside* of the method.  Make it a class-wide variable, but don't initialize it or use it until you need to.

Then, in the button click event, do something like this:

if ( textbox != null && !string.IsNullOrEmpty( textbox.Text ) )
{
   string myText = textbox.Text;
}
0
 
LVL 9

Accepted Solution

by:
rawinnlnx9 earned 2000 total points
ID: 34974789
If you are doing this but the contents are empty in your TextBox configuration area add another line:

textbox.AutoPostback=true;

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34974834
You can loop through the Controls collection of the form, checking for TextBox types. You'll need some way of determining which one is the one you're interested in though:
foreach (Control ctl in this.Controls)
{
    TextBox tb = ctl as TextBox;

    if (tb != null)
    {
        // Need some condition to tell if this is
        //  the text box you want
        if (tb.Name == "mySnazzyTextBox")
        {
            // do something

            return;  // break out of loop when found
        }
    }
}

Open in new window

0
 

Author Comment

by:rciner313
ID: 34975153
is there a way to name the text boxes before they are created? I am taking a value from the previous form and using that value to create the text boxes.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34975466
You already are naming them with line 56 in your OP.
0
 

Author Comment

by:rciner313
ID: 34975805
correct but when i go to the button click event i do not know how to retrieve those names to get the values.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34979146
Ok. I understand. Like this:
private void button1_Click(object sender, EventArgs e)
{
    TextBox tb = sender as TextBox;

    if (tb != null)
    {
        // tb.Name
    }           
}

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question