誤った値が入力され、検出された後にクリアされたフォーム値をクリアして表示する方法

StackOverflow https://stackoverflow.com/questions/4360547

  •  08-10-2019
  •  | 
  •  

質問

誤った値が入力され検出された後、クリアされたフォーム値をクリアして表示(リセット)する方法を見つけようとしています。現在、誤った入力をキャッチすると、Enterボタンを再度クリックした後でも、そこに座っています。どんな助けも大歓迎です。

  namespace Mileage
  {
    public partial class Form2 : Form
    {
      private double beginMileage, endMileage, gallons, mpg;        

      public Form2()
      {
        InitializeComponent();
      }

      //Enter button click
      public void menuItem1_Click(object sender, EventArgs e)
      {
        if (endMileage<beginMileage)
        {
            this.label5.Text = String.Format("ERROR: End mileage is less than begining mileage.");  
        }

        else if((endMileage<0)||(beginMileage<0))
        {
             this.label5.Text = String.Format("ERROR: One or more mileage input is negative.");                 
        }

        else if ((endMileage == 0) || (gallons == 0))
        {
            this.label5.Text = String.Format("ERROR: The end mileage and/or gallon input is zero.");

        }

        else 
        {
            beginMileage = double.Parse(this.textBox1.Text.Replace(" ", ""));

            endMileage = double.Parse(this.textBox2.Text.Replace(" ", ""));

            gallons = double.Parse(this.textBox3.Text.Replace(" ", ""))  ;

            mpg = ((endMileage - beginMileage) / gallons);

            this.label5.Text = String.Format("{0}", mpg);
        }

    }

    //exit button click
    public void menuItem2_Click(object sender, EventArgs e)
    {
        Application.Exit();            

    }
}

}

役に立ちましたか?

解決

うーん。 。 。だから私はそれを理解しました。それは私がやっただけのロジックエラーでした:)

namespace Mileage
{
  public partial class Form2 : Form
  {
    private float beginMileage, endMileage, gallons, mpg;        

    public Form2()
    {
        InitializeComponent();
    }

    public void menuItem1_Click(object sender, EventArgs e)
    {
        beginMileage = float.Parse(this.textBox1.Text.Replace(" ", ""));

        endMileage = float.Parse(this.textBox2.Text.Replace(" ", ""));

        gallons = float.Parse(this.textBox3.Text.Replace(" ", ""));

        if((endMileage<0)||(beginMileage<0)||(gallons<0))
        {
             this.label5.Text = String.Format("ERROR: One or more input(s) is negative.");
             this.textBox1.Text = " ";
             this.textBox2.Text = " ";
             this.textBox3.Text = " ";   
        }

        else if ((endMileage == 0) || (gallons == 0))
        {
            this.label5.Text = String.Format("ERROR: The end mileage and/or gallon input is zero.");
            this.textBox1.Text = " ";
            this.textBox2.Text = " ";
            this.textBox3.Text = " ";                 
        }

        else if (endMileage < beginMileage)
        {
            this.label5.Text = String.Format("ERROR: End mileage is less than begining mileage.");
            this.textBox1.Text = " ";
            this.textBox2.Text = " ";
            this.textBox3.Text = " ";

        }
        else 
        {                               
            mpg = ((endMileage - beginMileage) / gallons);
            this.label5.Text = String.Format("{0}", mpg);
        }

    }

    public void menuItem2_Click(object sender, EventArgs e)
    {
        Application.Exit();            

    }
}

}

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top