63 lines
2.3 KiB
C#
63 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
|
|
namespace CalcForm
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void ComputeButton_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (OpeComboBox.SelectedIndex == -1)
|
|
throw new Exception("Please select a valid operator");
|
|
|
|
double lValue = 0;
|
|
double rValue = 0;
|
|
|
|
if (string.IsNullOrWhiteSpace(LeftTextBox.Text) || string.IsNullOrWhiteSpace(RightTextBox.Text))
|
|
throw new Exception("Please fill the left and right text entries");
|
|
|
|
if (double.TryParse(LeftTextBox.Text, out lValue) == false)
|
|
throw new Exception(LeftTextBox.Text + " is not a valid number");
|
|
if (double.TryParse(RightTextBox.Text, out rValue) == false)
|
|
throw new Exception(RightTextBox.Text + " is not a valid number");
|
|
|
|
string selectedOp = OpeComboBox.SelectedItem as string;
|
|
if ((string.CompareOrdinal(selectedOp, "/") == 0 || string.CompareOrdinal(selectedOp, "%") == 0) && rValue == 0)
|
|
throw new Exception("Division per zero");
|
|
|
|
double result = 0;
|
|
|
|
if (string.CompareOrdinal(selectedOp, "+") == 0)
|
|
result = lValue + rValue;
|
|
else if (string.CompareOrdinal(selectedOp, "-") == 0)
|
|
result = lValue - rValue;
|
|
else if (string.CompareOrdinal(selectedOp, "/") == 0)
|
|
result = lValue / rValue;
|
|
else if (string.CompareOrdinal(selectedOp, "*") == 0)
|
|
result = lValue * rValue;
|
|
else if (string.CompareOrdinal(selectedOp, "%") == 0)
|
|
result = lValue % rValue;
|
|
|
|
ResultLabel.Text = "Result: " + result;
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "An error occurred");
|
|
ResultLabel.Text = "Result: Nothing";
|
|
}
|
|
}
|
|
}
|
|
}
|