enter image description here我使这段代码能够注册几个项目,但我真的不知道如何从文本框中收集文本。我收到了错误
NullReferenceException未处理
我知道这意味着某些东西没有初始化,但我无法弄清楚哪一个以及如何初始化它。错误行是“n = Convert.ToDecimal(element.Text);”在代码的最后。
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();
}
}
}