8 Replies - 1492 Views - Last Post: 11 May 2014 - 07:32 AM Rate Topic: -----

#1 piter_irvin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-May 14

Creating "X" number of textboxes at runtime and manipulate

Posted 06 May 2014 - 07:18 AM

i need to creat a program in la user give me "X" number and creat the textbox and y need sum all textbox

int t = Int32.Parse(txtbano.Text); 
           
            for (int i = 0; i < t; i++)
            {
                
                TextBox l = new TextBox();
                
                l.Name = "textBox" + i.ToString();
                l.Location = new Point(baseX, baseY + (35 * i));
                l.Text = "" + i.ToString();
               
                this.Controls.Add(l);
            }

This post has been edited by modi123_1: 06 May 2014 - 07:20 AM
Reason for edit:: use the 'code' button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: Creating "X" number of textboxes at runtime and manipulate

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15260
  • View blog
  • Posts: 61,138
  • Joined: 12-June 08

Re: Creating "X" number of textboxes at runtime and manipulate

Posted 06 May 2014 - 07:20 AM

Would you be more specific on the problem? Is your code not doing what you asked? If so what is it doing versus what should it be doing?
Was This Post Helpful? 0
  • +
  • -

#3 piter_irvin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-May 14

Re: Creating "X" number of textboxes at runtime and manipulate

Posted 06 May 2014 - 08:01 AM

the variable t is the number you textboxes I need, in the for loop is created but need to change the value of the textboxes and add them, example t = 2 make 2 boxes and create a sum button and does nothing

        public void Form2_Load(object sender, EventArgs e)
        {
            int baseX = 40;
            int baseY = 30;
            int t = Int32.Parse(txtbano.Text); 
            // creamos el array
            for (int i = 0; i < t; i++)
            {
                // crea el componente para ese elemento del array
                System.Windows.Forms.TextBox l = new System.Windows.Forms.TextBox();
                // la posiciona (una encima de otra)
                l.Name = "textBox" + i.ToString();
                l.Location = new Point(baseX, baseY + (35 * i));
                l.Text = "" + i.ToString();
                // agrega al array de controles de la ventana la etiqueta actual
                this.Controls.Add(l);
            }
        }
private void btnsuma_Click(object sender, EventArgs e)
        {
           int n;
           n = Int32.Parse(textBox1.Text) + Int32.Parse(textBox2.Text);
            txttotal.Text = n.ToString(); 
        }



Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15260
  • View blog
  • Posts: 61,138
  • Joined: 12-June 08

Re: Creating "X" number of textboxes at runtime and manipulate

Posted 06 May 2014 - 08:05 AM

Have you tried to have the sum button cycle through the 'this.controls' for the textboxes?
Was This Post Helpful? 0
  • +
  • -

#5 piter_irvin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-May 14

Re: Creating "X" number of textboxes at runtime and manipulate

Posted 06 May 2014 - 08:39 AM

i dont know, how I do that?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15260
  • View blog
  • Posts: 61,138
  • Joined: 12-June 08

Re: Creating "X" number of textboxes at runtime and manipulate

Posted 06 May 2014 - 08:41 AM

"this.Controls" is a collection, right? Perhaps iterate through it like any other collection - with a loop! A for loop would be fine.
Was This Post Helpful? 1
  • +
  • -

#7 piter_irvin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-May 14

Re: Creating "X" number of textboxes at runtime and manipulate

Posted 06 May 2014 - 10:18 AM

i do that, but need suggestion

l.TextChanged += new System.EventHandler(this.EvenText_Click);
this.Controls.Add(l);

private void EvenText_Click(object sender, EventArgs e)
        {
            TextBox text = sender as TextBox;
            s = s + Int32.Parse(text.Text);
}

private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("total " + s);
            s = 0;
        }


Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7052
  • View blog
  • Posts: 23,970
  • Joined: 05-May 12

Re: Creating "X" number of textboxes at runtime and manipulate

Posted 06 May 2014 - 04:45 PM

Look at your line 16 in your original post in your post #3. It's a collection. You can iterate over that same collection to find the textboxes that you added. http://msdn.microsof...(v=vs.110).aspx
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Creating "X" number of textboxes at runtime and manipulate

Posted 11 May 2014 - 07:32 AM

Well, you have a couple serious logic problems here.

Look at the Load() method

Quote

public void Form2_Load(object sender, EventArgs e)
        {
            int baseX = 40;
            int baseY = 30;
            int t = Int32.Parse(txtbano.Text);
            // creamos el array
            for (int i = 0; i < t; i++)




How are you doing to give the user time to enter a value in the textbox if you already react to the value at load time? In other words, if the default value of the textbox is 6 that's how many buttons you get because they are made as soon as the form loads.


The next biggest problem is that you are using the textboxes as your variables. You are doing all your calculations based on their values. That's very very bad. The GUI is only supposed to be a way to interact with the logic and properties in the code behind the GUI. Your logic class(es) really shouldn't know anything about the GUI or its textboxes. You should be able to take your logic/work/controller class and use it in a console application, Forms application, WPF application or Web application with virtually no changes. If you can't do that, then you have tightly bound your logic to your GUI and that's bad.


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1