Solved

getting information from dynamically added text boxes.

Posted on 2011-02-24
8
187 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
  • 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 500 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 25
abstract class C# 1 31
Please explain purpose of GZIP 4 35
custom authorization on controller action and HTML in asp.net mvc 1 27
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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