0

さまざまなオブジェクトの面積を計算しようとしています。これらすべてのオブジェクトには独自WinForm.のクラスがあります。クラスで計算を実行したいのですCalculationBaseが、それを実行しようとするとFormatException.、コードを介してメッセージが表示され続けます。

私のProgram.cs中で、計算したいフォームへの参照を作成します。

static class Program
    {
        public static frmRectangle formRect;
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            formRect = new frmRectangle();

            Application.Run(new frmMain());
        }
    }

CalculationBaseクラスでは、変数を次のように宣言し、double breadth, height;次のコードを挿入して計算を行いました。

private void Calculations()
        {
            FormCheck();
            if (rect)
            {

                Debug.WriteLine("Rectangular is open");

                // Area
                Program.formRect.txtFormArea.Text = string.Format("A = {0} * {1}", b, h);
                if (millimeters)
                    i = (int)Math.Pow(10, 2);
                else if (centimeters)
                    i = (int)Math.Pow(10, 4);

                areaPrimair = Math.Round((b * h), 4);
                areaSecundair = Math.Round((areaPrimair / i), 4);

                Program.formRect.txtAreaPrimair.Text = areaPrimair.ToString();
                Program.formRect.txtAreaSecundair.Text = areaSecundair.ToString();
            }
        }

この関数は ClickEvent で呼び出されます。

private void btnCalculate_Click(object sender, EventArgs e)
        {
                try
                {
                    breadth = double.Parse(Program.formRect.txtBreadthInput.Text);
                    height = double.Parse(Program.formRect.txtHeightInput.Text);

                    Calculations();
                }
                catch(FormatException)
                {
                    MessageBox.Show("Some values are invalid. Please check your input.", "Invalid value");
                }
            }
        }

前に述べたように、私のプログラムは FormatException のメッセージを表示し続けます。

4

1 に答える 1

1

に空の文字列または数値以外の文字列を渡すと Double.Parse("")、formatexception が発生します。ボタンを押す前に、二重文字列を入力していることを確認してください

于 2012-09-28T07:33:30.163 に答える