C# ListBox新增、修改、移除、清空的方法是什么

在C#中,ListBox控件提供了以下方法来实现新增、修改、移除和清空操作:

新增项:

使用ListBox.Items.Add()方法将新项添加到ListBox中。

或者使用ListBox.Items.Insert()方法将新项插入到指定的索引位置。

修改项:

使用ListBox.Items[index]属性来获取或设置特定索引位置的项的值。

移除项:

使用ListBox.Items.Remove()方法按值移除指定的项。

或者使用ListBox.Items.RemoveAt()方法按索引移除指定的项。

或者使用ListBox.Items.Clear()方法移除所有的项。

示例代码如下:

// 新增项
listBox1.Items.Add("Item 1");
listBox1.Items.Insert(1, "Item 2");
// 修改项
listBox1.Items[0] = "Updated Item";
// 移除项
listBox1.Items.Remove("Item 1");
listBox1.Items.RemoveAt(0);
listBox1.Items.Clear();

注意:以上示例代码中的listBox1是指ListBox控件的实例。你需要根据实际情况将其替换为你的ListBox控件的名称。

阅读剩余
THE END