Solved

getting information from dynamically added text boxes.

Posted on 2011-02-24
8
189 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 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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