首页  >  文章  >  后端开发  >  C# 中的列表框

C# 中的列表框

WBOY
WBOY原创
2024-09-03 15:28:17683浏览

C#中的ListBox定义为将元素列表添加到ListBox中以对单个或多个元素进行操作。下拉框和列表框的区别在于下拉框一次只能选择一个元素,而列表框则可以一次选择单个或多个元素。 ListBox 提供不同类型的方法、属性和事件。此列表框是在 System 下指定的。 Windows.Forms 包(命名空间)。

ListBox 类再次包含 C# 中 3 种不同类型的集合。他们是

  1.  ListBox.ObjectCollection:此集合类包含 ListBox 控件的所有元素。
  2.  ListBox.SelectedObjectCollection:此集合类保存 ListBox 控件中所选项目的集合。
  3.  ListBox.SelectedIndexCollection: 此集合类保存选定索引的集合;这些元素是 ListBox.ObjectCollection 索引的子集,并且是 ListBox 控件中专门选择的索引。

C# 中列表框的类型?

  1. 单选列表框:ListBox 只能从列表中选择单个元素。
  2. 多选ListBox:ListBox可以从列表中选择多个元素。

C# 中 ListBox 的先决条件:

  • .Net 库必须安装在您的电脑上
  • Visual Studio 设置

如何用 C# 创建列表框?

ListBox 可以通过两种方式创建:

  • 设计时
  • 运行时

1.设计时

最初无需任何代码即可轻松创建。创建项目的步骤

第 1 步:打开 Visual Studio

点击文件=>新建=>项目

C# 中的列表框

选择=>Windows Form Application,然后

请参阅下图以更好地了解项目结构:

C# 中的列表框

为项目命名并单击“确定”,您将获得如下所示的 Form1.cs(Design) 选项卡

C# 中的列表框

第2步:在视觉工作室左侧或从视图中,选择“工具箱”;接下来,将所需的元素拖放到 Form1.cs(Design) 上,如上图所示。

C# 中的列表框

C# 中的列表框

第 3 步:拖放后,从 Visual Studio 右侧选择属性,并为 Text 属性指定一些名称。这用于在 2nd 方法运行时.

中编写代码

C# 中的列表框

输出:

C# 中的列表框

2.运行时

这不是直接按照上面的方法来做。我们已经编写了一些程序来创建ListBox。这很简单;首先,拖放所有必需的元素,如 ListBox、Label、TextField、Button 等。如果双击任何拖放的元素,我们会得到一些元素操作方法的 C# 代码,我们必须将逻辑写入我们想要的内容。想要用这些元素来做。创建运行时项目代码以创建 ListBox

的步骤

第 1 步:使用 ListBox() 构造函数创建 ListBox 控件。

语法:

ListBox listBox = new ListBox();

第2步:创建ListBox属性后,如果我们想给ListBox的元素设置Font、Font.Size、Color等属性

语法:

listBox.Location = new Point(200, 100);
listBox.Size = new Size(100, 90);
listBox.ForeColor = Color.Red;

第 3 步:将元素添加到 ListBox。

语法:

listBox.Items.Add("A");
listBox.Items.Add("B");
listBox.Items.Add("C");
listBox.Items.Add("D");

第 4 步:将此列表框添加到表单中。

语法:

this.Controls.Add(listBox);

C# 中的列表框示例

以下是下面提到的示例

示例#1 – 创建列表框并添加元素

代码:

//importing C# required libraries
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 is project name
namespace WindowsFormsApplication26
{
//creating class extends from Form class
public partial class Form1 : Form
{
//constrcutor
public Form1()
{
//initializing components
InitializeComponent();
//Creating list box and add some properties and values to the List Box
listBox2.ForeColor = Color.Red;
listBox2.Items.Add("Java");
listBox2.Items.Add("Python");
listBox2.Items.Add("C++");
listBox2.Items.Add("C");
listBox2.Items.Add("C#");
listBox2.Items.Add("Spring");
listBox2.Items.Add("JavaFX");
listBox2.SelectionMode = SelectionMode.MultiSimple;
}
//method for selectedIndex change operation
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}

输出:

C# 中的列表框

示例 #2 – 用户输入值,通过单击按钮将其添加到列表框中

代码:

//importing C# required libraries
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 is project name
namespace WindowsFormsApp25
{
//creating class extends from Form class
public partial class Form1 : Form
{
//constrcutor
public Form1()
{
//initializing components
InitializeComponent();
}
//saving the enter values into List box
private void buttonSave_Click(object sender, EventArgs e)
{
//If user enter any values then if block executes
if (this.textBoxName.Text != "")
{
NameList.Items.Add(this.textBoxName.Text);
this.textBoxName.Focus();
this.textBoxName.Clear();
}
//If user did not enter any values then else block executes
else
{
MessageBox.Show("Please enter a name to add..","Error",MessageBoxButtons.OK,MessageBoxIcon.Information);
this.textBoxName.Focus();
}
}
}
}

输出:

输入值之前:

C# 中的列表框

在不输入任何值的情况下尝试单击保存按钮:

C# 中的列表框

输入值后:

C# 中的列表框

After entering a value and clicking the save button:

C# 中的列表框

Example #3 – Delete, Change the font of List Box values

Code:

//importing C# required libraries
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 is project name
namespace WindowsFormsApp25
{
//creating class extends from Form class
public partial class Form1 : Form
{
//constrcutor
public Form1()
{
//initializing components
InitializeComponent();
}
//saving the enter values into List box
private void buttonSave_Click(object sender, EventArgs e)
{
//If user enter any values then if block executes
if (this.textBoxName.Text != "")
{
NameList.Items.Add(this.textBoxName.Text);
this.textBoxName.Focus();
this.textBoxName.Clear();
}
//If user did not enter any values then else block executes
else
{
MessageBox.Show("Please enter a name to add..","Error",MessageBoxButtons.OK,MessageBoxIcon.Information);
this.textBoxName.Focus();
}
}
//Removing the selected elements
private void button2_Click(object sender, EventArgs e)
{
if (this.NameList.SelectedIndex >= 0)
{
this.NameList.Items.RemoveAt(this.NameList.SelectedIndex);
}
}
//Setting List box selected values font
private void button3_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
NameList.Font = fontDialog1.Font;
}
}
}
}

Output:

After adding 3 names:

C# 中的列表框

Deleting selected element:

C# 中的列表框

C# 中的列表框

Change the font of the values:

C# 中的列表框

C# 中的列表框

C# 中的列表框

C# 中的列表框

Conclusion

C# List box is used to add multiple elements to perform any specific operation. List Boxes are used to select a single value or multiple values at a time. In C# List Box can be created using Design-Time and Run-Time methods.

以上是C# 中的列表框的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Protected in C#下一篇:C# EventHandler