Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 84.6k

can't reach dynamically created controls

Dec 27 2021 10:35 AM

Hi

I created dynamically in panel2 a certain number of textbox depending of the value entered in the first textbox of panel1, and also a button and a lable. The purpose of this is to fill the created textbox with a number and to show the sum of all the numebers in the label.

My problem is that i don't know how to collect the entered value in each created textbox and how to create a button2_Click event for the created button2? When i click on button2, nothing happens. Thanks

using System;
using System.Drawing;
using System.Windows.Forms;

namespace oef1_array
{
    public partial class Form1 : Form
    {
        int quant = 0,sum=0;
        int  x= 0;
        string name="";
        public Form1()
        { InitializeComponent(); }

        private void textBox1_TextChanged(object sender, EventArgs e)
        { quant= Convert.ToInt32(textBox1.Text);  }

        private void button1_Click(object sender, EventArgs e)
        {
            int pointX = 30;
            int pointY = 40;
            panel2.Controls.Clear();
            for (int i = 0; i <quant; i++)
            {
                TextBox txt = new TextBox();
                txt.Name = "txt" + i.ToString();
                txt.Location = new Point(pointX, pointY);
                panel2.Controls.Add(txt);
                panel2.Show();
                pointY += 30;
            }

            Button bt = new Button();
            bt.Name = "button2";
            panel2.Controls.Add(bt);
            bt.Location = new Point(30, 30 * (quant+1)+20);
            bt.Text = "som";
            label1.Location = new Point(30, 30 * (quant + 1) + 50);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < quant; i++)
            {
                name = "txt" + i.ToString();
                //sum += name.Text;
                label1.Text = sum.ToString();
            }

        }
    }
}


Answers (3)