Search This Blog

Monday, November 1, 2010

Binding an event procedure with mutiple controls in c#.net using calculator program

Binding an event procedure with mutiple controls in c#.net

Objective:      Demo on binding an event procedure with mutiple controls in c#.net using calculator program

Design:

  

Design the form as above with 1 TextBox and 19 buttons.


Explaination:

 By binding an event procedure with mutiple controls having similar functionality we can reduce the no. Of lines of code in a program, as well as save our time.

In our application, when a we click a numeric button the respective number(text) should appear on the textbox.

Example for click event of btn0:
 ->for click event of btn0
textBox1.Text = btn0.Text;
 ->for click event of btn1
textBox1.Text = btn1.Text;

The code for click events of above two buttons is almost same, only the button names(btn0,btn1) are different.


*Binding an Event Procedure with multiple controls:

Instead of writing the similar code for all numeric buttons, we can write the code for the click event of a single button and bind it to all other buttons with similar functionality
i.e; write code for click event of btn0 and bind it with click events of btn1,btn2,...,btn9.

->Binding  Event Procedure of ‘btn0’with other numeric buttons:

      Select ‘btn0’, right click-> select ‘Properties’
      ->click on events icon
->select ‘Click’ event and double click,

      which generates click event for btn0, now write the code for btn0_click

Note: we can also generate click event of a button by double clicking on it.

->Now select the remaining numeric buttons on the form ,
  Go to Properties window -> select ‘events’ tab  -> select ‘Click’ event
  ->select ‘btn0_Click’


But if we do that, only the text of btn0 will appear on textbox when we click any of the numeric buttons.
Therefore, we have to recognize the button that had generated the event, read its text and display it on the textbox.

*When an event procedure is bound with mutiple controls, to recognize the control that has raised the event, we can make use of the parameter ‘sender’.
As ‘sender’ is of the type ‘object’, it will capture the object of the control that has raised the event.
We can refer to the members of that control by converting the ‘sender’ to appropriate control type.

Ex:
  private void btn0_Click(object sender, EventArgs e)
  {
   Button btn = (Button)sender;//converting the object 'sender' to control 'Button'
    textBox1.Text = btn.Text;              
  }

->Similarly bind the buttons btnAdd, btnSub, btnMult and btnDiv to a single event procedure, as these 4 buttons have similar code

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
           
            calc();
            oper = Convert.ToChar(btn.Text);           
            flag = true;                    
        }


-------------
Code:

using System;
using System.Windows.Forms;

namespace calc
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double res = 0; char oper; bool txt_changed=false, flag = false, clear_txtbox =false;
       

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = res.ToString();
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;//converting the object 'sender' to control 'Button'
            txt_changed = true;

            if (clear_txtbox == true || textBox1.Text == "0")
            {
                textBox1.Text = btn.Text;              
            }
            else
                textBox1.Text = textBox1.Text + btn.Text;
            clear_txtbox = false;

        }

        private void btnDot_Click(object sender, EventArgs e)
        {
            int index;
            index = textBox1.Text.IndexOf('.');
            if (index == -1 || clear_txtbox == true)
            {

                if (clear_txtbox == true || textBox1.Text == "0")
                {
                    textBox1.Text = "0.";
                }
                else
                    textBox1.Text = textBox1.Text + ".";
                clear_txtbox = false;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;           
            calc();
            oper = Convert.ToChar(btn.Text);           
            flag = true;                    
        }
        void calc()
        {
            if (textBox1.Text.Length > 0)
            {
                if (flag == true && txt_changed == true)
                {
                    switch (oper)
                    {
                        case '+':
                            res = res + Convert.ToDouble(textBox1.Text);
                            break;
                        case '-':
                            res = res - Convert.ToDouble(textBox1.Text);
                            break;
                        case '*':
                            res = res * Convert.ToDouble(textBox1.Text);
                            break;
                        case '/':
                            res = res / Convert.ToDouble(textBox1.Text);
                            break;
                        case '%':
                            res = res % Convert.ToDouble(textBox1.Text);
                            break;
                        default:
                            break;
                    }
                    textBox1.Text = res.ToString();
                }
                res = Convert.ToDouble(textBox1.Text);             
            }
            textBox1.Text = res.ToString();
            txt_changed = false;
            clear_txtbox = true;
        }
              
        private void btnEqualto_Click(object sender, EventArgs e)
        {
            calc();
        }
   
         private void btnBackspace_Click(object sender, EventArgs e)
        {
            if (txt_changed == true)
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
            if (textBox1.Text.Length == 0 && res == 0)
                textBox1.Text = res.ToString();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            res = 0;
            flag = false; clear_txtbox = false; txt_changed = false;
            textBox1.Text = res.ToString();
        }

    }
}