溫馨提示×

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

c#
小億
860
2023-10-18 16:45:44
欄目: 編程語言

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

  1. 新增項:
  • 使用ListBox.Items.Add()方法將新項添加到ListBox中。

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

  1. 修改項:
  • 使用ListBox.Items[index]屬性來獲取或設置特定索引位置的項的值。
  1. 移除項:
  • 使用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控件的名稱。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女