?
Solved

getting information from dynamically added text boxes.

Posted on 2011-02-24
8
Medium Priority
?
190 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

777 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