winforms 如何从多个文本框填充数组?

1rhkuytd  于 2022-12-19  发布在  其他
关注(0)|答案(3)|浏览(133)

我正在尝试从几个文本框填充一个数组,然后用数组中的值进行计算。
我有下面的代码,但不知道为什么它不工作。在代码中解释:

private void button1_Click(object sender, EventArgs e)
{
    double[] temperaturen = new double[6];

    temperaturen[0] = double.Parse(textBox1.Text);
   
    MessageBox.Show(temperaturen[0].ToString()); //the messagebox is just to test if 
                                                 //it works
}

上面的代码可以工作,但是当我从更多的文本框中添加更多的值后,它就停止工作了:

private void button1_Click(object sender, EventArgs e)
{
    double[] temperaturen = new double[6];
    
    temperaturen[0] = double.Parse(textBox1.Text); //now it stops working and gives 
    temperaturen[1] = double.Parse(textBox2.Text); //the following error:
    temperaturen[2] = double.Parse(textBox3.Text); //format exceptions was unhandled.
    temperaturen[3] = double.Parse(textBox4.Text);
    temperaturen[4] = double.Parse(textBox5.Text);
    temperaturen[5] = double.Parse(textBox6.Text);
    temperaturen[6] = double.Parse(textBox7.Text);

    MessageBox.Show(temperaturen[0].ToString());
}

有人知道从文本框解析是不是正确的方法吗?为什么这不起作用?

qlzsbp2j

qlzsbp2j1#

请注意,您有7个文本框,而不是6,因此您应该声明new double[7]。为了避免此类错误,让我们借助 Linq 进行查询:

using System.Linq;

...

double[] temperaturen = new TextBox[] {
  textBox1, textBox2, textBox3, 
  textBox4, textBox5, textBox6,
  textBox7 }
  .Select(box => double.TryParse(box.Text, out var value) ? value : double.NaN)
  .ToArray();

注意,该文本框可以包含 * 不是 * 有效浮点值的文本(例如,"bla-bla-bla"),在本例中我将double.NaNNota****Nnumber)放入其中。

k75qkfdt

k75qkfdt2#

真正解决问题的是:添加一个try catch,并将数组更改为具有7个值。
最后的代码,完全运行。

private void button1_Click(object sender, EventArgs e)
{
    decimal[] temperaturen = new decimal[7];//the array

    try // try block
    {
        temperaturen[0] = decimal.Parse(txtZondag.Text);//parsing input 
        temperaturen[1] = decimal.Parse(txtMaandag.Text);//from 
        temperaturen[2] = decimal.Parse(txtDinsdag.Text);//textboxes
        temperaturen[3] = decimal.Parse(txtWoensdag.Text);
        temperaturen[4] = decimal.Parse(txtDonderdag.Text);
        temperaturen[5] = decimal.Parse(txtVrijdag.Text);
        temperaturen[6] = decimal.Parse(txtZaterdag.Text);
        decimal temp = 10.2m;//decimal temp for calculations

        decimal uitkomst1 = 0.0m;//decimal for sum.
        uitkomst1 = temperaturen[0] - temp;//calculation
        textBox8.Text = uitkomst1.ToString();

        decimal uitkomst2 = 0;
        uitkomst2 = temperaturen[1] - temp;
        textBox9.Text = uitkomst2.ToString();

        decimal uitkomst3 = 0;
        uitkomst3 = temperaturen[2] - temp;
        textBox10.Text = uitkomst3.ToString();

        decimal uitkomst4 = 0.0m;
        uitkomst4 = temperaturen[3] = temp;
        textBox11.Text = uitkomst4.ToString();

        decimal uitkomst5 = 0;
        uitkomst5 = temperaturen[4] - temp;
        textBox12.Text = uitkomst5.ToString();

        decimal uitkomst6 = 0;
        uitkomst6 = temperaturen[5] - temp;
        textBox13.Text = uitkomst6.ToString();

        decimal uitkomst7 = 0;
        uitkomst7 = temperaturen[6] - temp;
        textBox14.Text = uitkomst7.ToString();
    }
    catch (Exception exception)//catch block
    {
        MessageBox.Show("Incorrecte invoer!  " + exception.Message);
    }
}
7lrncoxx

7lrncoxx3#

最后的代码,完全运行。

private void button1_Click(object sender, EventArgs e)
{
    decimal[] temperaturen = new decimal[7];//the array

    try // try block
    {
        temperaturen[0] = decimal.Parse(txtZondag.Text);//parsing input 
        temperaturen[1] = decimal.Parse(txtMaandag.Text);//from 
        temperaturen[2] = decimal.Parse(txtDinsdag.Text);//textboxes
        temperaturen[3] = decimal.Parse(txtWoensdag.Text);
        temperaturen[4] = decimal.Parse(txtDonderdag.Text);
        temperaturen[5] = decimal.Parse(txtVrijdag.Text);
        temperaturen[6] = decimal.Parse(txtZaterdag.Text);
        decimal temp = 10.2m;//decimal temp for calculations

        decimal uitkomst1 = 0.0m;//decimal for sum.
        uitkomst1 = temperaturen[0] - temp;//calculation
        textBox8.Text = uitkomst1.ToString();

        decimal uitkomst2 = 0;
        uitkomst2 = temperaturen[1] - temp;
        textBox9.Text = uitkomst2.ToString();

        decimal uitkomst3 = 0;
        uitkomst3 = temperaturen[2] - temp;
        textBox10.Text = uitkomst3.ToString();

        decimal uitkomst4 = 0.0m;
        uitkomst4 = temperaturen[3] = temp;
        textBox11.Text = uitkomst4.ToString();

        decimal uitkomst5 = 0;
        uitkomst5 = temperaturen[4] - temp;
        textBox12.Text = uitkomst5.ToString();

        decimal uitkomst6 = 0;
        uitkomst6 = temperaturen[5] - temp;
        textBox13.Text = uitkomst6.ToString();

        decimal uitkomst7 = 0;
        uitkomst7 = temperaturen[6] - temp;
        textBox14.Text = uitkomst7.ToString();
    }
    catch (Exception exception)//catch block
    {
        MessageBox.Show("Incorrecte invoer!  " + exception.Message);
    }
}

相关问题