0

enter image description hereI made this code to be able to register several items, but I don't really know how to collect the text from the textboxes. I got the error

NullReferenceException was unhandled

I know it means that something is not initialized but I can't figure out which one and how to initialize it. The error line is "n = Convert.ToDecimal(element.Text);" at the end of the code.

namespace TQ_ERP
{
    public partial class InvoiceForm : Form
    {
        Decimal tqty = 0;
        Decimal tamount = 0;
        static public int num = 1;

        TextBox[] textBoxesQ = new TextBox[100];
        TextBox[] textBoxesUP = new TextBox[100];
        TextBox[] textBoxesA = new TextBox[100];

        RichTextBox[] richTextBoxes = new RichTextBox[100];

        Label[] labels = new Label[100];

        public InvoiceForm()
        {
            InitializeComponent();
            l_itemnum.Text = num.ToString();
        }

        private void t_num_KeyDown(object sender, KeyEventArgs e)
        {
            int tester;
            bool flag = false;

            if (!Int32.TryParse(t_num.Text, out tester))
            {
                flag = false;
                return;
            }
            else
                flag = true;

            if (flag == true)
            {
                List<Button> buttons = new List<Button>();
                num = Convert.ToInt32(t_num.Text);

                if (num >= 2)
                {
                    for (int i = 1; i <= num; i++)
                    {
                        this.Size = new Size(900,250+100*i);

                        labels[i] = new Label();
                        textBoxesQ[i] = new TextBox();
                        textBoxesUP[i] = new TextBox();
                        textBoxesA[i] = new TextBox();
                        richTextBoxes[i] = new RichTextBox();
                    }

                    for (int i = 1; i <= num; i++)
                    {
                        this.Controls.Add(labels[i]);
                        labels[i].Size = new Size(37, 13);
                        labels[i].Location = new Point(15, 130 + 100 * i);
                        labels[i].Text = i.ToString();
                        this.Controls.Add(textBoxesQ[i]);
                        textBoxesQ[i].Size = new Size(66,20);
                        textBoxesQ[i].Location = new Point(310, 110 + 100 * i);
                        this.Controls.Add(textBoxesUP[i]);
                        textBoxesUP[i].Size = new Size(66, 20);
                        textBoxesUP[i].Location = new Point(410, 110 + 100 * i);
                        this.Controls.Add(textBoxesA[i]);
                        textBoxesA[i].Size = new Size(66, 20);
                        textBoxesA[i].Location = new Point(510, 110 + 100 * i);
                        this.Controls.Add(richTextBoxes[i]);
                        richTextBoxes[i].Size = new Size(183, 81);
                        richTextBoxes[i].Location = new Point(90, 110 + 100 * i);
                    }
                }
            }

            flag = false;
        }

        private void b_compute_Click(object sender, EventArgs e)
        {
            Decimal n=0;

            foreach (TextBox element in textBoxesQ)
            {
                ***n = Convert.ToDecimal(element.Text);***
                tqty += n;
            }

            t_tqty.Text = tqty.ToString();
            Decimal nup = Convert.ToDecimal(t_uprice.Text);
            tamount = tqty * nup;
            t_amount.Text = tamount.ToString();
        }
    }
}

0 Answers0