C#.net

Creating a Calculator in WPF with C# – Step-by-Step Tutorial

In this tutorials will teach how to make a calculator using wpf C# application step by step. How to create the calculator in wpf application in best coding practice.

First Step

Declare the variables

string CalTotal;
int num1;
int num2;
string option;
int result;

You have to paste the  below Codes

private void Button1_Click(object sender, RoutedEventArgs e)
{
    txtTotal.Text = txtTotal.Text + "1";
}

private void Button2_Click(object sender, RoutedEventArgs e)
{
    txtTotal.Text = txtTotal.Text + "2";
}

private void Button3_Click(object sender, RoutedEventArgs e)
{
    txtTotal.Text = txtTotal.Text + "3";
}

private void Button4_Click(object sender, RoutedEventArgs e)
{
    txtTotal.Text = txtTotal.Text + "4";
}

private void Button5_Click(object sender, RoutedEventArgs e)
{
    txtTotal.Text = txtTotal.Text + "5";
}

private void Button6_Click(object sender, RoutedEventArgs e)
{
    txtTotal.Text = txtTotal.Text + "6";
}

private void Button7_Click(object sender, RoutedEventArgs e)
{
    txtTotal.Text = txtTotal.Text + "7";
}

private void Button8_Click(object sender, RoutedEventArgs e)
{
    txtTotal.Text = txtTotal.Text + "8";
}

private void Button9_Click(object sender, RoutedEventArgs e)
{
    txtTotal.Text = txtTotal.Text + "9";
}

private void Button0_Click(object sender, RoutedEventArgs e)
{
    txtTotal.Text = txtTotal.Text + "0";
}

private void ButtonPlus_Click(object sender, RoutedEventArgs e)
{
    option = "+";
    num1 = int.Parse(txtTotal.Text);

    txtTotal.Clear();
}

private void ButtonMinus_Click(object sender, RoutedEventArgs e)
{
    option = "-";
    num1 = int.Parse(txtTotal.Text);

    txtTotal.Clear();
}

private void ButtonSub_Click(object sender, RoutedEventArgs e)
{
    option = "*";
    num1 = int.Parse(txtTotal.Text);

    txtTotal.Clear();
}

private void ButtonDiv_Click(object sender, RoutedEventArgs e)
{
    option = "/";
    num1 = int.Parse(txtTotal.Text);

    txtTotal.Clear();
}

private void ButtonClear_Click(object sender, RoutedEventArgs e)
{
    txtTotal.Clear();
    result = (0);
    num1 = (0);
    num2 = (0);
}

private void ButtonEql_Click(object sender, RoutedEventArgs e)
{
    num2 = int.Parse(txtTotal.Text);

    if (option.Equals("+"))
        result = num1 + num2;

    if (option.Equals("-"))
        result = num1 - num2;

    if (option.Equals("*"))
        result = num1 * num2;

    if (option.Equals("/"))
        result = num1 / num2;

    txtTotal.Text = result + "";
}

 

 

admin

Recent Posts

Flexbox: Build Responsive Navigation Bar with HTML & CSS

Introduction to Flexbox When it comes to building responsive layouts, Flexbox offers a powerful and…

5 days ago

Creating Stunning Image Gallery Using CSS Grid

Introduction to CSS Grid The CSS Grid Layout is a powerful tool for creating responsive…

5 days ago

Create Professional Login & Registration Form using HTML & CSS

In this tutorils we are going to teach how to make a attractive Login &…

4 weeks ago

Form Repeater using HTML CSS JQuery

In this tutorial we are going to teach Form Repeater using HTML CSS JQuery.step by…

1 month ago

AJAX CRUD Application in Laravel 11

Introduction to AJAX and Laravel 11 AJAX, which stands for Asynchronous JavaScript and XML which…

1 month ago

C#.net Banking Project: Accurate FD Rate Calculation Tutorial

Introduction to FD Rate Calculation In any banking project, accurately calculating Fixed Deposit (FD) rates…

1 month ago