0% found this document useful (0 votes)
9 views

Lab 5

This document discusses building a complex calculator demo project in Windows Forms for beginners. It describes adding single event handlers for multiple buttons to handle click events for numeric and operator buttons. A flag variable is used to write smart code to determine when the displayed value should be cleared. The steps provided design the user interface, add event handlers for the buttons, and include code for the calculation logic and display of results.

Uploaded by

lhtduong2100146
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab 5

This document discusses building a complex calculator demo project in Windows Forms for beginners. It describes adding single event handlers for multiple buttons to handle click events for numeric and operator buttons. A flag variable is used to write smart code to determine when the displayed value should be cleared. The steps provided design the user interface, add event handlers for the buttons, and include code for the calculation logic and display of results.

Uploaded by

lhtduong2100146
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Windows Forms for Beginners

Part 4

Demo Project:

• Complex Calculator

What will you learn?

• Adding single event handlers for Multiple Buttons


• Identifying
dentifying from which button handler is called.
• Using flag variable to write smart code.

Bhushan Mulmule
[email protected]
www.dotnetvideotutorial.com

www.dotnetvideotutorial.com
Demo Project 1: Complex Calculator

Step 1: Design UI:

Form: Label:
Name: frmCalculator Name: lblDisplay
Text: Calculator AutoSize: False
BackColor: White
BorderStyle: FixedSingle
Text: Blank
TextAlign: MiddleRight

2 Group Boxes:
Text: Blank

18 Buttons:
Text: 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
., C, +, -, *, /, =, OFF

Name: Name accordingly for


example btn1, btnPlus,
btnOFF, etc

Instructions:

• When clicked on any number button that number should get displayed on label
lblDisplay.
• As all numeric buttons have same functionality we will add single handler
Digit_Click for click event of all numeric buttons

www.dotnetvideotutorial.com
• As all operator buttons have same functionality we will add single handler
Operator_Click for click event of all operator buttons

Step 2: Adding event handlers

1. Right click on form  Click View code.


2. In class frmCalculator Declare following variables

public partial class frmCalculator : Form


{
double no1, no2, result;
string op;
bool flg;

…..
}

3. Insert single event handler for all numeric buttons.


• First Select number 1 button (btn1)
• Press Control key and then select all number buttons
• Go to property window  Switch to event view by clicking light sign (see
image below: pointed by Event Button call out)  Locate Click event 
Type “Digits_Click”  Press enter and handler will be inserted

Events Button

All number
buttons are
selected

Click Event

www.dotnetvideotutorial.com
4. In Digits_Click() handler write following code

private void Digits_Click(object sender, EventArgs e)


{
if (flg == true)
{
lblDisplay.Text = "";
flg = false;
}
Button btn = (Button)sender;
lblDisplay.Text += btn.Text;
}

5. Insert single event handler for all operator buttons


• Select Plus button then press control key and select Minus, Multiplication
and Division buttons.
• Do not select Equal (=) button
• Go to property window  Events list  Locate Click event and type
“Operator_Click()” and press enter.  code it as follow

private void Operator_Click(object sender, EventArgs e)


{
no1 = Convert.ToInt32(lblDisplay.Text);
op = ((Button)sender).Text;
flg = true;
}

6. Double click on equal button and insert following code

private void btnEqual_Click(object sender, EventArgs e)


{
no2 = Convert.ToDouble(lblDisplay.Text);
switch (op)
{
case "+":
result = no1 + no2;
break;
case "-":
result = no1 - no2;
break;
case "*":
result = no1 * no2;
break;
case "/":

www.dotnetvideotutorial.com
result = no1 / no2;
break;
}
lblDisplay.Text = result.ToString();
flg = true;
}

7. Double click on point button

private void btnPoint_Click(object sender, EventArgs e)


{
if(! lblDisplay.Text.Contains("."))
lblDisplay.Text += ".";
}

8. Clear button

private void btnClear_Click(object sender, EventArgs e)


{
no1 = 0;
no2 = 0;
result = 0;
lblDisplay.Text = "";
}

9. OFF button

private void btnOFF_Click(object sender, EventArgs e)


{
Application.Exit();
}

10. Execute and test.


11. Debug step by step using F11 to understand logic

www.dotnetvideotutorial.com

You might also like