您的位置:首页 > 其它

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

2017-03-01 21:04 337 查看
选择类控件--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控件被取消");  
            }
        }
       
    }
}
效果:


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐