Five Function Calculator Visual Programming in C Sharp

By | August 26, 2023

Five Function Calculator Visual Programming in C Sharp –

Develop a Windows Forms Application using Visual Studio and C Sharp. Design user interface of this program by adding 2 text boxes for number 1 and number 2, 4 label controls, 2 buttons for calculate and reset, 5 radio buttons with text addition, subtraction, multiplication, division and power.

Five <yoastmark class=


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClaculatorWithRadioButtons
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double n1, n2, result=0;
            n1 = double.Parse(textBox1.Text);
            n2 = double.Parse(textBox2.Text);
            if (radioButton1.Checked)
                result = n1 + n2;
            else if (radioButton2.Checked)
                result = n1 - n2;
            else if (radioButton3.Checked)
                result = n1 * n2;
            else if (radioButton4.Checked)
                result = n1 / n2;
            else if (radioButton5.Checked)
                result = Math.Pow(n1,n2);
            label4.Text = result.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            label4.Text = "";
        }
    }
}

Explanation of the key components of Five Function Calculator:

  1. The program is defined in the ClaculatorWithRadioButtons namespace and contains a partial class named Form1, which represents the main form of the application.
  2. The Form1 constructor (public Form1()) initializes the components of the form.
  3. The button1_Click event handler is executed when the “Calculate” button (likely named button1 by default) is clicked. It performs the arithmetic calculation based on user inputs and the selected radio button.
    • The numbers to be operated on (n1 and n2) are extracted from the text boxes (textBox1 and textBox2).
    • The if statements check which radio button is checked and performs the corresponding arithmetic operation (+, -, *, /, or exponentiation) using the Math.Pow() function.
    • The result is assigned to the result variable, and it is displayed in the label4 label.
  4. The button2_Click event handler is executed when the “Clear” button (likely named button2 by default) is clicked. It clears the text boxes and the result label.

When you run this program, it will display a Windows Form with text boxes for entering two numbers, radio buttons for selecting an arithmetic operation, and buttons for calculating the result and clearing the input. The selected operation will be performed on the provided numbers, and the result will be displayed in a label.

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *