知识问答

C#列表框、复选列表框、组合框的用法实例

C#列表框、复选列表框、组合框的用法实例

列表框(ListBox)的用法

基本用法

列表框是Windows Forms中的一个控件,用于在提供选项列表(一个或多个)的窗体或对话框中选择单个选项,它的常用属性有:

  • DataSource:列表框的数据源对象;
  • DisplayMember:指定数据绑定时显示的属性名;
  • ValueMember:指定数据绑定时使用的属性名;
  • SelectionMode:列表框的选择模式,可选用单选和多选两种模式。

例如:

DataTable dt = new DataTable();dt.Columns.Add(new DataColumn("ID", typeof(int)));dt.Columns.Add(new DataColumn("Name", typeof(string)));dt.Rows.Add(1, "Tom");dt.Rows.Add(2, "Jerry");dt.Rows.Add(3, "Lucy");this.listBox1.DataSource = dt;this.listBox1.DisplayMember = "Name";this.listBox1.ValueMember = "ID";this.listBox1.SelectionMode = SelectionMode.MultiSimple;

上面的代码用 DataTable 来作为数据源,设置了显示的属性为 Name,使用的属性为 ID,选择模式为 MultiSimple,这意味着用户可以选择多个选项,选择结果通过 SelectedValues 属性获取,如下所示:

foreach (object item in this.listBox1.SelectedItems){    DataRowView dr = item as DataRowView;    Console.WriteLine(dr.Row["ID"].ToString() + " " + dr.Row["Name"].ToString());}

列表框选项的添加、删除、清空

在不使用数据绑定的情况下,我们可以通过 Items 属性来添加、删除和清空列表框的选项。

this.listBox2.Items.Clear();this.listBox2.Items.Add("选项一");this.listBox2.Items.Add("选项二");this.listBox2.Items.Add("选项三");this.listBox2.Items.Add("选项四");this.listBox2.Items.RemoveAt(1);this.listBox2.Items.Remove("选项四");

复选列表框(CheckedListBox)的用法

基本用法

复选列表框是 Windows Forms 中的一个控件,是列表框的一个扩展,与列表框最大的不同就是本身就可支持多项选择。其常用属性有:

  • DataSource:复选列表框的数据源对象;
  • DisplayMember:指定数据绑定时显示的属性名;
  • ValueMember:指定数据绑定时使用的属性名;
  • CheckOnClick:指示是否单击项时就激活或取消复选框;
  • SelectionMode:复选列表框的选择模式,可选用单选和多选两种模式。

例如:

DataTable dt = new DataTable();dt.Columns.Add(new DataColumn("ID", typeof(int)));dt.Columns.Add(new DataColumn("Name", typeof(string)));dt.Rows.Add(1, "Tom");dt.Rows.Add(2, "Jerry");dt.Rows.Add(3, "Lucy");this.checkedListBox1.DataSource = dt;this.checkedListBox1.DisplayMember = "Name";this.checkedListBox1.ValueMember = "ID";this.checkedListBox1.CheckOnClick = true;this.checkedListBox1.SelectionMode = SelectionMode.MultiSimple;

上面的代码用 DataTable 来作为数据源,设置了显示的属性为 Name,使用的属性为 ID,启用了单击项就激活复选框,并且选择模式为 MultiSimple,这意味着用户可以选择多个选项,选择结果通过 CheckedItems 属性获取,如下所示:

foreach (object item in this.checkedListBox1.CheckedItems){    DataRowView dr = item as DataRowView;    Console.WriteLine(dr.Row["ID"].ToString() + " " + dr.Row["Name"].ToString());}

复选列表框选项的添加、删除、清空

在不使用数据绑定的情况下,我们可以通过 Items 属性来添加、删除和清空复选列表框的选项。

this.checkedListBox2.Items.Clear();this.checkedListBox2.Items.Add("选项一");this.checkedListBox2.Items.Add("选项二");this.checkedListBox2.Items.Add("选项三");this.checkedListBox2.Items.Add("选项四");this.checkedListBox2.Items.RemoveAt(1);this.checkedListBox2.Items.Remove("选项四");

组合框(ComboBox)的用法

基本用法

组合框是 Windows Forms 中的一个控件,它能够处理一个下拉列表和一个用户输入的文本框。当用户单击组合框时,其下拉列表中的项将会弹出。如果用户在文本框中键入数值,组合框将会根据显示样式自动格式化文本。它的常用属性有:

  • DataSource:组合框的数据源对象;
  • DisplayMember:指定数据绑定时显示的属性名;
  • ValueMember:指定数据绑定时使用的属性名;
  • DropDownStyle:指定组合框中的下拉列表的样式,可选用三种下拉列表方式,Simple,DropDown和DropDownList三种;
  • Text:组合框文本框中的文本字符串;
  • Items:组合框中所有项的集合。

例如:

DataTable dt = new DataTable();dt.Columns.Add(new DataColumn("ID", typeof(int)));dt.Columns.Add(new DataColumn("Name", typeof(string)));dt.Rows.Add(1, "Apple");dt.Rows.Add(2, "Banana");dt.Rows.Add(3, "Orange");this.comboBox1.DataSource = dt;this.comboBox1.DisplayMember = "Name";this.comboBox1.ValueMember = "ID";this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;this.comboBox1.SelectedIndex = 1;

上面的代码用 DataTable 来作为数据源,设置了显示的属性为 Name,使用的属性为 ID,选择样式为 DropDownList,这意味着用户不能输入任意文本,只能从数据源中的选项列表中选择。同时设置了默认选择索引为1,即“Banana”这一选项。

组合框选项的添加、删除、清空

在不使用数据绑定的情况下,我们可以通过 Items 属性来添加、删除和清空组合框的选项。

this.comboBox2.Items.Clear();this.comboBox2.Items.Add("选项一");this.comboBox2.Items.Add("选项二");this.comboBox2.Items.Add("选项三");this.comboBox2.Items.Add("选项四");this.comboBox2.Items.RemoveAt(1);this.comboBox2.Items.Remove("选项四");

示例一

创建一个程序,用列表框选择一个图片,再通过PictureBox控件把选择的图片显示出来。

首先要在项目的“引用”中添加“System.Drawing”命名空间:

using System.Drawing;

在Form中添加一个列表框控件和一个PictureBox控件,代码如下:

private void Form1_Load(object sender, EventArgs e){    // 增加图片选项    DirectoryInfo dir = new DirectoryInfo("Images");    foreach (FileInfo file in dir.GetFiles())    {        if (file.Extension.ToLower().Equals(".jpg"))        {            this.listBox1.Items.Add(file.Name);        }    }}private void listBox1_SelectedIndexChanged(object sender, EventArgs e){    string filename = "Images\\" + this.listBox1.SelectedItem.ToString();    Image img = Image.FromFile(filename);    this.pictureBox1.Image = img;}

上面的代码首先增加图片选项到列表框中,然后在 SelectedIndexChanged 事件中获取选择项的文件名,再通过 Image.FromFile 方法读取图片文件,最后显示到 PictureBox 控件上。

示例二

创建一个程序,用组合框选择不同字体,并可通过控制RadioButton和CheckBox控件来设置其字体大小和样式。

在Form中添加ComboBo控件、RadioButton控件和CheckBox控件3个控件,代码如下:

private void Form1_Load(object sender, EventArgs e){    foreach (FontFamily ff in FontFamily.Families)    {        this.comboBox1.Items.Add(ff.Name);    }    this.comboBox1.SelectedIndex = 0;    this.radioButton1.Checked = true;}private void comboBox1_SelectedIndexChanged(object sender, EventArgs e){    Font font = new Font(this.comboBox1.SelectedItem.ToString(), 16);    if (this.radioButton1.Checked)    {        font = new Font(this.comboBox1.SelectedItem.ToString(), 16, FontStyle.Regular);    }    else if (this.radioButton2.Checked)    {        font = new Font(this.comboBox1.SelectedItem.ToString(), 16, FontStyle.Bold);    }    else if (this.radioButton3.Checked)    {        font = new Font(this.comboBox1.SelectedItem.ToString(), 16, FontStyle.Italic);    }    if (this.checkBox1.Checked) font = new Font(font, FontStyle.Underline);    if (this.checkBox2.Checked) font = new Font(font, FontStyle.Strikeout);    this.label1.Font = font;}

上面的代码首先加载了所有系统字体到组合框中,然后在 SelectedIndexChanged 事件中根据各个控件的状态创建字体对象,并将其作为 Label 的字体。其中主要的代码如下:

Font font = new Font(this.comboBox1.SelectedItem.ToString(), 16);if (this.radioButton1.Checked){    font = new Font(this.comboBox1.SelectedItem.ToString(), 16, FontStyle.Regular);}else if (this.radioButton2.Checked){    font = new Font(this.comboBox1.SelectedItem.ToString(), 16, FontStyle.Bold);}else if (this.radioButton3.Checked){    font = new Font(this.comboBox1.SelectedItem.ToString(), 16, FontStyle.Italic);}if (this.checkBox1.Checked) font = new Font(font, FontStyle.Underline);if (this.checkBox2.Checked) font = new Font(font, FontStyle.Strikeout);this.label1.Font = font;

其中首先通过 ComboBox 控件选择的字体名称来创建 Font 对象,然后根据 RadioButton 控件选择的样式,设置字体对象的样式。最后根据 CheckBox 控件确定是否需要设置 Underline 和 Strikeout 样式。