Sunday 8 May 2011

Radio Button and Check Boxes







Radio Button
      Radio buttons themselves as a label with a dot to the left of it, which can be either selected or not. You should use the radio buttons when you want to give the user a choice between several mutually exclusive options. for Example, if you want to ask for the gender of the user.
    To group radiobuttons together so that they create one logical unit you must use a GroupBox control. By first placing a group box on a form, and then placing the RadioButton controls you need within the borders of the group box, the RadioButton controls will know to change their state to reflect that only one within the group box can be selected. If you do not place them within a group box, only one RadioButton on the form can be selected at any given time.
CheckBox Controls
    A CheckBox traditionally displays itself as a label with a small box with a checkmark to the left of it. You should use the check box when you want to allow the user to choose one or more options. An example could be a questionnaire asking which operating systems the user has tried (for example, Windows 95, Windows 98, Linux, Max OS X, and so on.)
RadioButton Properties
Name
Availability
Description
Appearance Read/Write A RadioButton can be displayed either as a label with a circular check to the left, middle or right of it, or as a standard button. When it is displayed as a button, the control will appear pressed when selected and 3D otherwise.
AutoCheck Read/Write When this property is true, a check mark is displayed when the user clicks the radio button. When it is false, the check mark is not displayed by default.
CheckAlign Read/Write By using this property, you can change the alignment of the radio button. It can be left, middle, and right.
Checked Read/Write Indicates the status of the control. It is true if the control has a check mark, and false otherwise.
RadioButton Events
Name
Description
CheckChanged This event is sent when the check of the RadioButton changes. If there is more than one RadioButton control on the form or within a group box, this event will be sent twice, first to the control, which was checked and now becomes unchecked, then to the control which becomes checked.
Click This event is sent every time the RadioButton is clicked. This is not the same as the change event, because clicking a RadioButton twice or more times in succession only changes the checked property once � and only if it wasn't checked already.
CheckBox Properties
Name
Availability
Description
CheckState Read/Write Unlike the RadioButton, a CheckBox can have three states: Checked, Indeterminate, and Unchecked. When the state of the check box is Indeterminate, the control check next to the label is usually grayed, indicating that the current value of the check is not valid or has no meaning under the current circumstances. An example of this state can be seen if you select several files in the Windows Explorer and look at their properties. If some files are ReadOnly and others are not, the ReadOnly checkbox will be checked, but grayed indeterminate.
ThreeState Read/Write When this property is false, the user will not be able to change the CheckBox' state to Indeterminate. You can, however, still change the state of the check box to Indeterminate from code.
CheckBox Events
Name
Description
CheckedChanged Occurs whenever the Checked property of the check box changes. Note that in a CheckBox where the ThreeState property is true, it is possible to click the check box without changing the Checked property. This happens when the check box changes from checked to indeterminate state.
CheckedStateChanged Occurs whenever the CheckedState property changes. As Checked and Unchecked are both possible values of the CheckedState property, this event will be sent whenever the Checked property changes. In addition to that, it will also be sent when the state changes from Checked to Indeterminate.

Example:  In following image i have created a window form for saving information of student like personal info and subjects of his/her. For this you have to drag and drop 4 textboxes, 2 radiobuttons, 5 labels and 3 buttons. Change all  controls properties like in picture.



















In figure 2 if user direct click on ok button then he/she will get massage for alert to fill all information. In Figure 3 if user fill personal information and then click on Ok button then he/she will get alert message for choosing gender. And in figure 4 if user not choose subject and click on ok button then he  will get another alert message for choosing subject. To making all these validation you have to write following code on Ok button Click.
Code For Ok Button Click:


 private void button2_Click(object sender, EventArgs e)
        {
           
            if (textBox1.Text == "" || textBox2.Text == "" || textBox4.Text == "")
            {
                MessageBox.Show("Please fill all personal information", "Warning", 
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }
            else if (radioButton1.Checked == false && radioButton2.Checked == false)
            {
                MessageBox.Show("Please select Gender", "Warning", MessageBoxButtons.OKCancel,
MessageBoxIcon.Warning);
            }
            else if (checkedListBox1.Text=="")
            {
                MessageBox.Show("Please select Subjects", "Warning", MessageBoxButtons.OKCancel,

 MessageBoxIcon.Warning);
            }

            else
            {

                if (radioButton1.Checked)
                {

                    // Concatenate the text values of the four TextBoxes
                    output = "Name: " + this.textBox1.Text + "\r\n";
                    output += "Address: " + this.textBox2.Text + "\r\n";
                    output += "Course: " + this.textBox4.Text + "\r\n";
                    output += "Sex: " + this.radioButton1.Text + "\r\n";
                    output += "Subject:";
                    foreach (string sub in checkedListBox1.CheckedItems)
                        output += "  " + sub;

                }
                if (radioButton2.Checked)
                {

                    // Concatenate the text values of the four TextBoxes
                    output = "Name: " + this.textBox1.Text + "\r\n";
                    output += "Address: " + this.textBox2.Text + "\r\n";
                    output += "Course: " + this.textBox4.Text + "\r\n";
                    output += "Sex: " + this.radioButton2.Text+"\r\n";
                    output += "Subject:";
                    foreach (string sub in checkedListBox1.CheckedItems)
                        output += "  " + sub;
                }
                this.textBox3.Text = output;
            }
        }
                Now if you to give condition for user that he should  must choose at least  three subjects then you can add code for this on Checklistbox Leave Event. Like following code.
Code for Leave Event on Checklist Box.
 


private void checkedListBox1_Leave(object sender, EventArgs e)
        {
            int count = checkedListBox1.CheckedItems.Count;
            {   
                if(count<=3)
                MessageBox.Show("Your minimum choice is Three subject");
            }
        }
Final output will show like in below figure-

No comments:

Post a Comment