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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Selva GanapathyRank #560 · 4.3K reputationPosted Apr 7, 2014, 2:01 AM EDT
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 SharmaRank #174 · 13.9K reputationPosted Apr 4, 2014, 1:04 AM EDT