Experiment-11 Create windows form Application to design GUI for complex
Calculator and develop it using C#.
using System;
using [Link];
namespace ComplexCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
private void label1_Click(object sender, EventArgs e)
{
private int GetInput(TextBox textBox)
{
if ([Link]([Link], out int value))
return value;
else
throw new Exception("Please enter valid integers.");
}
private void button1_Click(object sender, EventArgs e)
{
try
{
int num1 = GetInput(textBox1);
int num2 = GetInput(textBox2);
int result = num1 + num2;
[Link] = $"{result}";
}
catch (Exception ex)
{
[Link]([Link]);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
int num1 = GetInput(textBox1);
int num2 = GetInput(textBox2);
int result = num1 - num2;
[Link] = $"{result}";
}
catch (Exception ex)
{
[Link]([Link]);
}
private void button3_Click(object sender, EventArgs e)
{
try
{
int num1 = GetInput(textBox1);
int num2 = GetInput(textBox2);
int result = num1 * num2;
[Link] = $"{result}";
}
catch (Exception ex)
{
[Link]([Link]);
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
int num1 = GetInput(textBox1);
int num2 = GetInput(textBox2);
int result = num1 / num2;
[Link] = $"{result}";
}
catch (Exception ex)
{
[Link]([Link]);
}
}
private void button5_Click(object sender, EventArgs e)
{
[Link]();
[Link]();
[Link]();
}
}
}
Explanation
Name Purpose
using System; Includes the System namespace, which contains basic
Name Purpose
classes and base types (like Exception, Int32, etc.).
Imports the Windows Forms library to create GUI
using [Link];
(Graphical User Interface) applications.
Defines a namespace named ComplexCalculator to
namespace ComplexCalculator
organize and encapsulate related classes.
Declares a partial class Form1 that inherits from Form
— the base class for Windows Forms. The partial
public partial class Form1 : Form
keyword allows the class definition to be split across
multiple files.
Constructor for the Form1 class. It initializes the form
public Form1()
components by calling InitializeComponent().
Event handler that executes when the form loads.
private void Form1_Load(object
(Currently empty but can be used to set initial values or
sender, EventArgs e)
properties.)
private void label1_Click(object Event handler for when the label (likely a UI element)
sender, EventArgs e) is clicked. (Currently empty.)
A helper method that takes a TextBox control as input,
private int GetInput(TextBox
attempts to convert its text to an integer, and returns it.
textBox)
Throws an exception if conversion fails.
Event handler for the Addition button (+). It retrieves
button1_Click(object sender,
two numbers from text boxes, adds them, and displays
EventArgs e)
the result in another text box.
button2_Click(object sender, Event handler for the Subtraction button (-). It retrieves
EventArgs e) two numbers, subtracts them, and shows the result.
Event handler for the Multiplication button (*). It
button3_Click(object sender,
retrieves two numbers, multiplies them, and displays
EventArgs e)
the result.
Event handler for the Division button (/). It divides the
button4_Click(object sender,
first number by the second and shows the result.
EventArgs e)
Handles exceptions (like divide-by-zero).
button5_Click(object sender, Event handler for the Clear button. It clears all input
EventArgs e) and output text boxes.
Displays an error message in a dialog box if an
[Link]([Link]) exception occurs (e.g., invalid input or division by
zero).
GUI elements (TextBoxes) used for user input and
textBox1, textBox2, textBox3
displaying output.
Output: