选择类控件有下拉组合框(ComboBox)控件、复选框(CheckBox)控件、单选按钮(RadioButton)控件、数值选择(NumreicUpDown)控件和列表(ListBox)控件。
下拉组合框控件被称为下拉组合框控件,是由System.Windows.Forms.ComboBox类提供的,主要作用是将一个集合数据以组合框的形式显示给用户,当用户单击时将以下拉框显示给用户供用户从中选择一项。
1、创建拉下框并添加内容
(1)直接单击控件上方的小箭头,然后再单击“编辑项”,就会弹出 “字符串集合编辑器”对话框。
(2)在属性面板中选择Items属性,然后单击...按钮,也能弹出“字符串集合编辑器”对话框。
(3)在代码中添加ComboBox控件的内容。
编写程序,将ComboBox控件DropDownStyle的属性设置为DropDownList,并向控件中添加3项内容,使其为只可以进行选择操作的下拉框。
生成Form1的Load事件:
输入代码之前:
输入代码之后 :
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.Items.Add("语文");
comboBox1.Items.Add("数学");
comboBox1.Items.Add("化学");
}
}
}
运行结果如下:
DropDownStyle属性有3个属性值,这3个属性值对应不同的样式。
2、选中下拉组合框中可编辑部分的所有文本
当控件的DropDownStyle属性设置为DropDown时,再使用SelectAll方法,就可以选择ComboBox控件可编辑部分的所有文本
编写程序,将DropDownStyle属性设置为DropDown,并向控件添加3项内容,在选择下拉列表中的某项时,单击“选择”按钮,就会调用控件的SelectAll方法,实现可编辑框中的内容被选中。
生成botton1的Click事件:
输入代码之前:
输入代码之后 :
Form1的Load事件,代码也修改了,如同上面。
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
comboBox1.Items.Add("语文");
comboBox1.Items.Add("数学");
comboBox1.Items.Add("化学");
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.SelectAll();
}
}
}
运行结果如下:
不明白什么意思。
复选框控件,允许用户选择和清除关联选项。与单选按钮不同的是,复选框无论是处于同一个容器中还是在不同的容器中,都是允许多选的。
判断复选框是否被选中
代码之前:
代码之后:
代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void checkBox1_Click(object sender, EventArgs e)
{
if(checkBox1.CheckState==CheckState.Checked)
{
MessageBox.Show("选中成功");
}
else
{
MessageBox.Show("取消选中");
}
}
}
}
对checkBox控件的CheckState属性进行判断,当该属性值为Checked时,则表明该控件处于选中状态;否则就处于未选中状态。
CheckState属性有以下3个值。
运行结果如下:
实现CheckBox控件的多项选择
CheckBox的Text属性修改为游泳,电影,绘画,唱歌。
button的Text属性修改为显示信息
代码之前:
代码之后:
放错的位置
代码内容:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
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 checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
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 + " ";
}
}
}
}
}
运行结果如下:
单选按钮控件,当与其它单选按钮成对出现时,允许用户从一组选项中选择单个选项。也就是说,当同一个容器中存在两个以上的单选按钮时,只能有一个被选中。但不在同一个容器中的几组单选按钮彼此不关联,是可以有多个被选中的。
使用RadioButton控件完成一道 选择题。
添加两个Label控件
添加四个RadioButton控件
一个Button控件:
Form1发生Load事件
四个radioButton控件发生CheckedChanged事件:
Button控件发生Click事件:
运行结果如下:
Button的Text属性改为:提交答案
再次运行结果 :
数值选择控件又称为数值选择控件,是一个显示和输入数值的控件,控件提供了一对上下箭头,主要作用是将一个数按一定的值进行增加或减少。它主要有四个常用的属性:
(1)Increment属性表示每次单击按钮时增加或者减少的量。
(2)Maximum属性表示最大值。
(3)Minumum属性表示最小值。
(4)Value属性表示当前值。
注意:如果想要使NumericUpDown控件的值发生更改时,就会响应事件ValueChange。
编写程序,向窗体添加NumbericUpDown控件,并使用Label控件在ValueChange事件中输出当前值。
添加NumberiUpDown和Label的两个控件。
Form1控件发生Load事件:
numbericUpDown1控件发生ValueChanged事件:
完整代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
numericUpDown1.Maximum = 50; //设置控件的最大值为50
numericUpDown1.Minimum = 0; //设置控件的最小值为0
numericUpDown1.Increment = 5; //设置单击按钮时增加或者减少的数值为5
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
label1.Text = "当前值为:" + numericUpDown1.Value;
}
}
}
运行结果如下:
列表控件又称列表框,它显示一个项目列表供用户选择。在列表框中,用户一次可以选择一项,也可以选择多项。
ListBox控件与ComboBox控件添加选项的方法一致,都是通过“编辑项”或者在Items属性中打开“字符串集合编辑器”对话框输入选项。
1、在ListBox控件中添加和移除项
在实现ListBox控件的添加和删除之前,用户需要熟悉以下属性和方法。
1)常用属性
Items属性:用于存放列表框中的列表项,是一个集合。通过该属性,可以添加列表项、移除列表项和获得列表项的数目。
SelectedItems属性:获取ListBox控件中选定项的集合。通常将该属性设置为SelectedItems.Count,用于获取包含在当前选定的项的集合。
2)常用方法
Items.Add方法:用来向列表框中增添一个列表项。调用格式及功能如下:
ListBox对象.Items.Add(s);
用于把参数s添加到“listBox对象”指定的列表框的列表项中。
Items.Remove方法:用来从列表框中删除一个列表项。调用格式及功能如下:
ListBox对象.Items.Remove(s);
用于从ListBox对象指定的列表框中删除列表项s。
Items.Clear方法:用来清除列表框中的所有项。其调用格式如下:
ListBox对象.Items.Clear();
编写程序,在窗体中添加一个ListBox控件,并通过Add方法向该控件添加选项,再通过Remove方法删除选项。
添加一个ListBox控件
添加一个TextBox控件:
添加三个Button控件:
button1、button2、button3,在Text属性修改为:添加、删除、清空列表。
在button1_Click事件中,使用ListBox控件的Add方法,添加选项内容
代码之前
代码之后
在button2_Click事件中,使用ListBox控件的Remove方法,删除多余的内容
在button3_Click事件中,使用ListBox控件的Clear方法,清空该控件的所有内容。
Form_Load事件
完整代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入要添加的选项");
}
else
{
listBox1.Items.Add(textBox1.Text);
textBox1.Text = "";
}
}
private void button2_Click(object sender, EventArgs e)
{
if(listBox1.SelectedItems.Count==0)
{
MessageBox.Show("请选择要删除的选项");
}
else
{
listBox1.Items.Remove(listBox1.SelectedItem);
}
}
private void button3_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "添加";
button2.Text = "删除";
button3.Text = "清空列表";
}
}
}
运行结果:
输入文字:
点击添加:
2、在列表框中选择多项
在实现ListBox控件中选择多项之前,用户需要熟悉一下SelectionMode属性。该属性是用来获取或设置在ListBox控件中选择列表项的方法。
SelectionMode属性值:
(1)属性性为MultiExtended时,按下Shift键的同时单击鼠标或者同时按Shift键和箭头键之一,会将选定内容从前一选定项扩展到当前项。按Ctrl键的同时单击鼠标将选择或撤消选择列表中的某项。
(2)属性值为MultiSimple时,鼠标单击或按空格键将选择或撤消选择列表中的某项。
(3)该属性的默认值 为One,即只能选择一项。
(4)属性值为None时,表示无法选择项。
编写程序,将SelectionMode属性值 设置为MultiExtended,实现在控件中可以选择多项。
添加一个ComboBox控件,并输入相应的选项
添加ListBox
添加Label控件
添加两个Button控件,button1和button2的text属性分别改为是《添加》和《查看选择数量》。
在button1_Click事件中,使用ListBox控件的Add方法,将ComboBox控件中的选项依次添加到ListBok控件中。接着,用户可以按下Shift键或Ctrl键和箭头键来进行选择。最后在button2_Click事件中,输入所选中课程的个数。
运行结果如下:
选择英语,再按添加
代码不对了,检查 一下代码,都没有错。重新运行: