Solved

getting information from dynamically added text boxes.

Posted on 2011-02-24
8
184 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 19

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
 
LVL 74

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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 74

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 74

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now