C#--窗体控件(选择类控件)

选择类控件有下拉组合框(ComboBox)控件、复选框(CheckBox)控件、单选按钮(RadioButton)控件、数值选择(NumreicUpDown)控件和列表(ListBox)控件。

下拉组合框控件

下拉组合框控件,是由System.Windows.Forms.ComboBox类提供的,主要作用是将一个集合数据以组合框的形式显示给用户,当用户单击时将以下拉框显示给用户供用户从中选择一项。

复选框控件

复选框控件,允许用户选择和清除关联选项。与单选按钮不同的是,复选框无论是处于同一个容器中还是在不同的容器中,都是允许多选的。
CheckState属性有以下3个值。① Checked表明该控件处于选中状态。② Indeterminate表明该控件处于不确定状态。一个不确定的控件通常具有灰色的外观。③ Unchecked表明该控件处于未选中状态。
编写程序,实现CheckBox控件的多项选择

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;  //多行显示
            richTextBox1.SelectionBullet = true;
            richTextBox1.Text = "姓名:张三"+"\n"+ "兴趣爱好:";
            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)
        {

        }
    }
}

【程序分析】在代码中首先,创建一个长度为4的CheckBox类型的数组,用来保存兴趣爱好;接着再将对象的指针依次传递给数组;最后将RichTextBox控件设置成多行和段落显示,并通过for循环将兴趣爱好存入到RichTextBox控件中。
在这里插入图片描述

单选按钮控件

单选按钮控件,当与其他单选按钮成对出现时,允许用户从一组选项中选择单个选项。也就是说,当同一个容器中存在两个以上的单选按钮时,只能有一个被选中。但不在同一个容器中的几组单选按钮彼此不关联,是可以有多个被选中的。注意:RadioButton和CheckBox控件的属性、事件基本是一样的。
编写程序,使用RadioButton控件完成一道选择题。

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 = "选择题:第二次鸦片战争的事件?";
            radioButton1.Text = "A:1856 至 1860年";
            radioButton2.Text = "B:1853 至 1856年";
            radioButton3.Text = "C:1840 至 1842年";
            radioButton4.Text = "D:1883 至 1885年";
        }
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            label2.ForeColor = Color.Blue;
            if (radioButton1.Checked)
            {
                label2.Text = "你的答案是:" + radioButton1.Text;
            }
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            label2.ForeColor = Color.Blue;
            if (radioButton2.Checked)
            {
                label2.Text = "你的答案是:" + radioButton2.Text;
            }
        }
        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            label2.ForeColor = Color.Blue;
            if (radioButton3.Checked)
            {
                label2.Text = "你的答案是:" + radioButton3.Text;
            }
        }
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            label2.ForeColor = Color.Blue;
            if (radioButton4.Checked)
            {
                label2.Text = "你的答案是:" + radioButton4.Text;
            }           
        }
        private void button1_Click(object sender, EventArgs e)
        {
            label2.ForeColor = Color.Red;
            if (radioButton1.Checked)
                label2.Text = "恭喜你,回答正确";
            else
                label2.Text = "对不起,回答错误";
            
        }
    }
}

在这里插入图片描述

【程序分析】本例演示了RadioButton控件的使用。在窗体中首先添加两个Label控件、四个RadioButton控件和一个Button控件;然后在代码中,为Label控件和RadioButton控件输入相应的文本;接着在RadioButton控件的CheckedChanged事件中依次对四个单选按钮控件进行判断;最后在Button控件的Click事件中,输出正确答案。
在这里插入图片描述
在这里插入图片描述

数值选择控件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值