さまざまなオブジェクトの面積を計算しようとしています。これらすべてのオブジェクトには独自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 のメッセージを表示し続けます。