Checked Changed Event

this is the default event of both the above 2 controls which occurs when the controls are selected as well as de-selected also. To work with the event design a form as following



  • Change the name of ‘Name’ TextBox as txtName, ‘Total Fees’ TextBox as txtFees, ‘Reset Form’ Button as btnReset and ‘Close Form’ button as btnClose.
  • Change the ReadOnly property of ‘Total Fees’ TextBox as true so that it becomes non editable, enter ‘0’ as a value in Text property and also set the TextAlign property as right
  • Set the Tag property for each CheckBox and RadioButton with their corresponding fees values and it should be ‘0’ for Normal RadioButton. Tag property is used for associating user defined data to any control just like Text property, but Text value is visible to end user and Tag value is not visible to end user
  • Click on any one CheckBox so that CheckedChanged Event procedure gets generated; bind that event procedure with all the remaining CheckBox’s.
  • Click on any one RadioButton so that CheckedChanged Event procedure gets generated; bind that event procedure with all the remaining RadioButton’s.
  • Now go to Code View and write the following code.

  • Class Declarations

       int count = 0;
        

    Code under CheckedChanged Event Procedure of all CheckBox’s

       radioButton1.Checked = true;
    int amt = int.Parse(txtFees.Text);
    CheckBox cb = sender as CheckBox;
    if (cb.Checked) {
    	count += 1;
    	amt += Convert.ToInt32(cb.Tag);
    } else {
    	count -= 1;
    	amt -= Convert.ToInt32(cb.Tag);
    }
    txtFees.Text = amt.ToString();
        

    Code under CheckedChanged Event Procedure of all RadioButton’s:

        int amt = int.Parse(txtFees.Text);
    RadioButton rb = sender as RadioButton;
    if (rb.Checked)
       amt += (Convert.ToInt32(rb.Tag) * count); else
       amt -= (Convert.ToInt32(rb.Tag) * count);
    txtFees.Text = amt.ToString();
        

    Code under Click Event Procedure of Reset Form Button


        foreach (Control ctrl in groupBox1.Controls) {
    	CheckBox cb = ctrl as CheckBox;
    	cb.Checked = false;
    }
    foreach (Control ctrl in this.Controls) {
    	if (ctrl.GetType().Name == "TextBox"){
    		TextBox tb = ctrl as TextBox;
    		tb.Clear();
    	}
    }
    txtFees.Text = "0";
    txtName.Focus(); 

    Code under Click Event Procedure of Reset Form Button:

    this.Close();