In this article we will see how to bind a single event to multiple controls and also use of the tag property. With this article we will clearly see how we can avoid redundancy of code and how to apply re-usability.
STEP1:-Design form as follows
STEP2:-Apply TAG values as follows to each control as follows
STEP3:-Create a event private void checkBox_CheckedChanged
And then bind each checkbox to the event we created.
Do the same thing for the Radio buttons creating radioButton_CheckedChanged event.
Code is as follows:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms; namespace WindowsProject{ public partial class Form6 : Form { int counter = 0; public Form6() { InitializeComponent(); } private void checkBox_CheckedChanged(object sender, EventArgs e) { CheckBox cb = sender as CheckBox; int amt = int.Parse(txtAmount.Text); if (cb.Checked) { counter += 1; amt += Convert.ToInt32(cb.Tag); } else { counter -= 1; amt -= Convert.ToInt32(cb.Tag); ; } txtAmount.Text = amt.ToString(); if (counter == 0) radioButton1.Checked = true; } private void radioButton_CheckedChanged(object sender, EventArgs e) { int amt = int.Parse(txtAmount.Text); if (amt > 0) { RadioButton rb = sender as RadioButton; if (rb.Checked) amt += Convert.ToInt32(rb.Tag); else amt -= Convert.ToInt32(rb.Tag); txtAmount.Text = amt.ToString(); } else radioButton1.Checked = true; } }}
Programming Windows Common Dialogs with Visual C# 2010