/*_______________________________________________________________ * E V E N T H A N D L E R F O R M U L T I P L E C O N T R O L S * * Ron Kessler * ______________________________________________________________ */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Event_handler_for_Multiple_Controls { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } /*--handle all click events from one handler. Notice it is not obvious! Create a new handler * by double-clicking the first radiobutton. Rename the handler. Set the click event for * all the buttons in the methods page (F4 then lightning bolt). Select this handler for all the * click events for each control. Look in the Designer.cs of your form to see the code for each * button. It points to this handler when any of the buttons is clicked. */ private void RadioButton_Click(object sender, EventArgs e) { //---use sender to find out which button was clicked! RadioButton whichButton = (RadioButton)sender; //create a reference variable of radiobutton type switch (whichButton.Name) { case "rdoYes": MessageBox.Show("You picked Yes", "Voting results", MessageBoxButtons.OK, MessageBoxIcon.Information); break; case "rdoNo": MessageBox.Show("You picked No", "Voting results", MessageBoxButtons.OK, MessageBoxIcon.Information); break; case "rdoNotSure": MessageBox.Show("You picked Not Sure", "Voting results", MessageBoxButtons.OK, MessageBoxIcon.Information); break; } } private void btnQuit_Click(object sender, EventArgs e) { this.Close(); } } }