返回博客

C井 WinFrom图形化开发 listBox列表

分类:学习记录 作者:樱振宇 发布时间:2026-06-10 14:54:53
// 1. 清空所有内容
listBox1.Items.Clear();

// 2. 添加一条数据
listBox1.Items.Add("WiFi名称");

// 3. 批量添加数组(最常用)
listBox1.Items.AddRange(数组);

// 4. 添加列表
listBox1.Items.AddRange(list.ToArray());

// 5. 获取选中的内容
string 选中 = listBox1.SelectedItem.ToString();

// 6. 获取选中的索引
int 索引 = listBox1.SelectedIndex;

// 7. 获取总条数
int 总数 = listBox1.Items.Count;

// 8. 根据索引获取某一条
string 内容 = listBox1.Items[索引].ToString();

// 9. 删除某一条
listBox1.Items.RemoveAt(索引);

// 10. 判断是否选中
if (listBox1.SelectedIndex != -1) { }

// 11. 自动适应列宽(让文字显示完整)
listBox1.HorizontalScrollbar = true;