Simple calcuator control in WPF
there are many logic can build a calculator , is any simple logic to perform calculation?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Selva GanapathyPosted Apr 7, 2014, 6:01 AM
Here is a main logic to develop calculator.
namespace Simple_Sample
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
String Operator = String.Empty;
String Operant = String.Empty;
bool isNewValue = false ;
private void TextBox_TextChanged_1(object sender, TextChangedEventArgs e)
{
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if (isNewValue)
{
Operant = this.OperantDisplay.Text;
this.OperantDisplay.Text = String.Empty;
}
this.OperantDisplay.Text += (sender as Button).Content;
isNewValue = false;
}
void ExecuteOperation(String operant1, String operant2, String oper)
{
if (oper == "+")
this.OperantDisplay.Text = (Convert.ToInt16(operant1) + Convert.ToInt16(operant2)).ToString();
else if (oper == "-")
this.OperantDisplay.Text = (Convert.ToInt16(operant1) - Convert.ToInt16(operant2)).ToString();
else if (oper == "*")
this.OperantDisplay.Text = (Convert.ToInt16(operant1) * Convert.ToInt16(operant2)).ToString();
else if (oper == "/")
this.OperantDisplay.Text = (Convert.ToInt16(operant1) / Convert.ToInt16(operant2)).ToString();
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
this.OperatorDisplay.Text = (sender as Button).Content.ToString ();
if (Operator != string.Empty && Operant!= string.Empty)
{
ExecuteOperation(Operant, this.OperantDisplay.Text, Operator);
}
if (this.OperatorDisplay.Text != "=")
{
this.Operator = this.OperatorDisplay.Text;
}
else
{
this.OperatorDisplay.Text = String.Empty;
}
isNewValue = true;
}
}
}
Regards,
Selva Ganapathy K
Munesh SharmaPosted Apr 4, 2014, 5:04 AM