C # checkbox and RadioButton

Source: Internet
Author: User

Typically Radiobox is called a radio button, and a checkbox is called a multi-select button, both of which derive from the ButtonBase class and can be treated as a button.

The selection between multiple checkboxes is independent of each other and is complementary to each other. Multiple RadioButton are mutually exclusive and can only be selected. Multiple radiobutton under the same container are mutually exclusive, and RadioButton objects from different containers are relatively independent.

Both the RadioButton and CheckBox controls have a checked property, and if the control is selected, the value of the Checked property is true otherwise false. The CheckedChanged event is raised when the selection state is changed, and the selection state of the control can be learned in real time by this event.

1. Create a window that uses the checkbox and Radiobox controls

2. Add two Label controls (used as information output)

3. Add a checkbox to share processing events checkedchanged the event when a change occurs

4, add the following code in oncheckchanged

  Private voidOncheckchanged (Objectsender, EventArgs e) {displaycheckresults ();//calling a custom method        }        Private voiddisplaycheckresults () {if(Label1! =NULL)            {                //Create a list<string> instancelist<string> strlist =Newlist<string>(); //adds the contents of the Text property of the selected checkbox to the list                if(checkbox1.checked) Strlist.add (Checkbox1.text); if(checkbox2.checked) Strlist.add (Checkbox2.text); if(checkbox3.checked) Strlist.add (Checkbox3.text); //The character concatenation of all elements of a concatenated string array, where the specified delimiter is used between each element. //Public static string Join (string separator, params string[] value);                stringres =string. Join (",", Strlist.toarray ()); //Determine if all are not selected and if all are not selected clear Label1.Text                if(checkbox1.checked = =false) && (checkbox2.checked = =false) && (checkbox3.checked = =false) ) Label1. Text=""; Else                 //replaces one or more format items in the specified string with the string representation of the specified object. Label1. Text =string. Format ("selected: {0}", RES); }        }

5. Add click Sharing Event in RadioButton

6. Enter the code in the shared event

  Private voidOnClick (Objectsender, EventArgs e) {            if(RadioButton1. Checked) Label2. Text=string. Format ("{0}", Radiobutton1.text); Else if(radiobutton2.checked) Label2. Text=string. Format ("{0}", Radiobutton2.text); Else  if(radiobutton3.checked) Label2. Text=string. Format ("{0}", Radiobutton3.text); ElseLabel2. Text =""; }

7. Operation Effect

Tick the corresponding box to pop the corresponding character.

C # checkbox and RadioButton

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.