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.
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:
- The program is defined in the
ClaculatorWithRadioButtons
namespace and contains a partial class namedForm1
, which represents the main form of the application. - The
Form1
constructor (public Form1()
) initializes the components of the form. - The
button1_Click
event handler is executed when the “Calculate” button (likely namedbutton1
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
andn2
) are extracted from the text boxes (textBox1
andtextBox2
). - The
if
statements check which radio button is checked and performs the corresponding arithmetic operation (+
,-
,*
,/
, or exponentiation) using theMath.Pow()
function. - The result is assigned to the
result
variable, and it is displayed in thelabel4
label.
- The numbers to be operated on (
- The
button2_Click
event handler is executed when the “Clear” button (likely namedbutton2
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.