Windows Forms For Beginners Part - 4

506 views

Published on

Demo Project:
• Complex Calculator

What will you learn?
• Adding single event handlers for Multiple Buttons
• Identifying from which button handler is called.
• Using flag variable to write smart code.

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
506
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
45
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Windows Forms For Beginners Part - 4

  1. 1. Demo Project: • Complex Calculator What will you learn? • Adding single event handlers • Identifying from which button handler is called. • Using flag variable to write smart Windows Forms for Beginners www.dotnetvideotutorial.com single event handlers for Multiple Buttons dentifying from which button handler is called. Using flag variable to write smart code. Bhushan Mulmule bhushan.mulmule@gmail.com www.dotnetvideotutorial.com Windows Forms for Beginners Part 4 Bhushan Mulmule bhushan.mulmule@gmail.com www.dotnetvideotutorial.com Windows Forms for Beginners
  2. 2. www.dotnetvideotutorial.com Demo Project 1: Complex Calculator Step 1: Design UI: 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 Label: Name: lblDisplay AutoSize: False BackColor: White BorderStyle: FixedSingle Text: Blank TextAlign: MiddleRight Form: Name: frmCalculator Text: Calculator 18 Buttons: Text: 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, ., C, +, -, *, /, =, OFF Name: Name accordingly for example btn1, btnPlus, btnOFF, etc 2 Group Boxes: Text: Blank
  3. 3. 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 Click Event All number buttons are selected
  4. 4. 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 "/":
  5. 5. 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

×