The selection controls include ComboBox control, check box control, radio button control, numericupdown control and ListBox control.
Drop down combo box control
The drop-down combo box control is provided by the System.Windows.Forms.ComboBox class. Its main function is to display a collection data to the user in the form of combo box. When the user clicks, the following drop-down combo box will be displayed to the user for the user to select one from.
Check box control
Check box control that allows the user to select and clear associated options. Unlike radio buttons, check boxes allow multiple selections whether they are in the same container or in different containers.
The CheckState property has the following three values. ① Checked indicates that the control is selected. ② Indeterminate indicates that the control is in an indeterminate state. An indeterminate control usually has a gray appearance. ③ Unchecked indicates that the control is unchecked.
Write a program to realize multiple choices of CheckBox control
using System; using System.Windows.Forms; namespace Form20 { public partial class Form1 : Form { CheckBox[] interests = new CheckBox[4]; public Form1() { InitializeComponent(); interests[0] = checkBox1; interests[1] = checkBox2; interests[2] = checkBox3; interests[3] = checkBox4; } private void button1_Click(object sender, EventArgs e) { richTextBox1.Multiline = true; //Multiline display richTextBox1.SelectionBullet = true; richTextBox1.Text = "Name: Zhang San"+"\n"+ "hobby:"; for (int i = 0; i < 4; i++) { if (interests[i].Checked) { richTextBox1.Text = richTextBox1.Text + interests[i].Text+" "; } } } private void Form1_Load(object sender, EventArgs e) { } } }
[program analysis] in the code, first, create an array of CheckBox type with length of 4 to save interests; Then pass the pointer of the object to the array in turn; Finally, set the RichTextBox control to multi line and paragraph display, and store the interests into the RichTextBox control through the for loop.
radio button
Radio button control, when paired with other radio buttons, allows the user to select a single option from a set of options. That is, when there are more than two radio buttons in the same container, only one can be selected. However, several groups of radio buttons not in the same container are not associated with each other, and multiple can be selected. Note: the properties and events of RadioButton and CheckBox controls are basically the same.
Write a program to complete a multiple-choice question using the RadioButton control.
using System; using System.Drawing; using System.Windows.Forms; namespace Form21 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { label1.Text = "Multiple choice: the second Opium War?"; radioButton1.Text = "A: 1856 To 1860"; radioButton2.Text = "B: 1853 To 1856"; radioButton3.Text = "C: 1840 To 1842"; radioButton4.Text = "D: 1883 To 1885"; } private void radioButton1_CheckedChanged(object sender, EventArgs e) { label2.ForeColor = Color.Blue; if (radioButton1.Checked) { label2.Text = "Your answer is:" + radioButton1.Text; } } private void radioButton2_CheckedChanged(object sender, EventArgs e) { label2.ForeColor = Color.Blue; if (radioButton2.Checked) { label2.Text = "Your answer is:" + radioButton2.Text; } } private void radioButton3_CheckedChanged(object sender, EventArgs e) { label2.ForeColor = Color.Blue; if (radioButton3.Checked) { label2.Text = "Your answer is:" + radioButton3.Text; } } private void radioButton4_CheckedChanged(object sender, EventArgs e) { label2.ForeColor = Color.Blue; if (radioButton4.Checked) { label2.Text = "Your answer is:" + radioButton4.Text; } } private void button1_Click(object sender, EventArgs e) { label2.ForeColor = Color.Red; if (radioButton1.Checked) label2.Text = "Congratulations, your answer is correct"; else label2.Text = "Sorry, wrong answer"; } } }
[program analysis] this example demonstrates the use of RadioButton control. First, add two Label controls, four RadioButton controls and one Button control to the form; Then, in the code, enter the corresponding text for the Label control and RadioButton control; Then judge the four radio Button controls in turn in the CheckedChanged event of the RadioButton control; Finally, in the Click event of the Button control, output the correct answer.