选择类控件-----ComboBox控件---CheckBox控件

选择类控件--ComboBox控件---CheckBox控件


1、ComboBox控件

(1)属性DropDownStyle:获取或设置指定组合框样式的值(有3个值)。

  • Simple:使ComboBox控件的列表部分总是可见的。
  • DropDown:DropDownStyle属性的默认值,只有单击右侧箭头才能显示列表部分。
  • DropDownList:用户不能编辑ComboBox控件文本部分,呈现下拉框的样式。
实例演示1:


为Form1窗体添加Load事件,其代码为Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 选择类控件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;  //设置DropDownStyle属性,设为下拉列表样式
            comboBox1.Items.Add("helloworld");		//向控件添加数据
            comboBox1.Items.Add("The second");
            comboBox1.Items.Add("come on!");

        }
    }

}

运行结果:

实例演示2:

为Form1窗体添加Load事件,为ComboBox控件添加SelectedValueChanged事件,其代码为Form1.cs:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 选择类控件 {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         private void Form1_Load(object sender, EventArgs e)         {             comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;//设置DropDownStyle属性,设为下拉列表样式             comboBox1.Items.Add("helloworld");             comboBox1.Items.Add("The second");             comboBox1.Items.Add("come on!");         }         private void comboBox1_SelectedValueChanged(object sender, EventArgs e)         {             label1.Text = comboBox1.Text;         }     } }

运行结果:

实例演示3:

将comboBox名字改为num_s,为Form1窗体添加Load事件.

ComboBox控件添加SelectedValueChanged事件,其代码为Form1.cs:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 选择类控件 {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         private void Form1_Load(object sender, EventArgs e)         {             num_s.DropDownStyle = ComboBoxStyle.DropDownList;//设置DropDownStyle属性,设为下拉列表样式             num_s.Items.Add("helloworld");                         //波特率             int[] baud = { 115200, 57600, 38400, 28800, 19200, 14400, 9600, 4800, 2400, 1200 };             for (int i = 0; i < 10; i++)             {                 num_s.Items.Add(baud[i]); //向控件循环添加数据             }             num_s.SelectedIndex = 7;    //默认选择第7项         }         private void comboBox1_SelectedValueChanged(object sender, EventArgs e)         {             label1.Text = num_s.Text; //label控件显示选中comboBox控件中的内容         }     } }

运行结果

2、CheckBox控件

(1)控件CheckState属性:Checked被选中;Unchecked取消选中状态

实例1:

为控件checkBox1添加Click事件,Form1.cs代码为:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 选择类控件 {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         private void checkBox1_Click(object sender, EventArgs e)         {             if (checkBox1.CheckState==CheckState.Checked)   //被选中             {                 MessageBox.Show("CheckBox控件被选中");             }             else                                          //取消选中             {                 MessageBox.Show("CheckBox控件被取消");               }         }             } }

效果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值