i make a program that create a number of texboxes equal of number that i insert in the first textbox,after i received the others number of the other texboxes in input and i make the sum of these numbers
The problem is that i insert in the first textbox "5" and in the other 5 texboxes i insert
"9" "8" "7" "6" "5" the output is 25

public partial class Multinomiale : Form
    {
        long N;
        int[] Vettore;
        TextBox txtTemp;
        public Multinomiale()
        {
            InitializeComponent();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Form_capitolo_1 f = new Form_capitolo_1();
            f.Show();
            this.Visible = false;
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            bool Tn = Controllo_TextBox(textBox1.Text);
            if (Tn == false)
            {
                MessageBox.Show("'n' deve essere un numero intero maggiore di 0!");
                textBox1.Text = "";
            }
            else if (Tn == true)
            {
                N = Convert.ToInt32(textBox1.Text);
                Vettore=new int[N];
                for (int i = 2; i < N+2; i++)
                {
                    string textname = "textBox_f" + i;
                    txtTemp = new TextBox();
                    txtTemp.Location = new System.Drawing.Point(50, 200 + 26 * i);
                    txtTemp.Size = new System.Drawing.Size(200, 25);
                    txtTemp.Name = textname;
                    txtTemp.Width = 100;
                    this.Controls.Add(txtTemp);
                }
                for (int i = 2; i < N + 2; i++)
                {
                    string labelname = "label_f" + i;
                    Label labelTemp = new Label();
                    labelTemp.Location = new System.Drawing.Point(15, 200 + 26 * i);
                    labelTemp.Text = " f" + (i - 1) + "=";
                    labelTemp.Font = new Font(labelTemp.Font, FontStyle.Bold);
                    labelTemp.Name = labelname;
                    this.Controls.Add(labelTemp);
                }
                for (int i = 2; i < N + 2; i++)
                {
                    string textname = "textBox_P" + i;
                    TextBox txtTemp = new TextBox();
                    txtTemp.Location = new System.Drawing.Point(200, 200 + 26 * i);                    
                    txtTemp.Size = new System.Drawing.Size(400, 25);
                    txtTemp.Name = textname;
                    txtTemp.Width = 100;
                    this.Controls.Add(txtTemp);     
                }
                label1.Enabled = false;
                textBox1.Enabled = false;
                button1.Enabled = false;
            }
        }

        static bool Controllo_TextBox(string S)
        {
            bool T = true;
            if (S[0] == 0)
                T = false;
            for (int i = 1; i < S.Length; i++)
            {
                if (S[i] != '0' && S[i] != '1' && S[i] != '2' && S[i] != '3' && S[i] != '4' && S[i] != '5' && S[i] != '6' && S[i] != '7' && S[i] != '8' && S[i] != '9')
                    T = false;
            }
            return T;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int somma = 0;
            for (int i = 0; i < N; i++)
            {
                Vettore[i] = Convert.ToInt32(txtTemp.Text);
                somma+= Vettore[i];
            }
            label2.Text = Convert.ToString(somma);
        }

Recommended Answers

All 4 Replies

in line 83 you are converting txtTemp.Text to an int, shouldn't you be converting your new textboxes.Text to ints?

You are adding 5 times the number 5 which adds up perfectly to 25.(See Momerath)
Computing machines are very dumb, the only do what you ask them to do. :)

After you create textboxes use the the foreach to loop through all controls, check if control is textbox, parse using TryParse to the desired type and add them together

That would work if he only had the textboxes on the screen that he wanted to add, but from his code you can see he has at least one other.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.