详解C#如何对ListBox控件中的数据进行操作

 更新时间:2024年03月18日 10:24:15   作者:wenchm  
这篇文章主要为大家详细介绍了C#中对ListBox控件中的数据进行的操作,主要包括添加、删除、清空、选择、排序等,感兴趣的小伙伴可以了解下

C#中对ListBox控件中的数据进行的操作主要包括添加、删除、清空、选择、排序等。

1.添加数据

// 添加一个字符串数组
listBox1.Items.AddRange(new string[] { "item1", "item2", "item3" });
 
// 或者添加单个字符串
listBox1.Items.Add("item4");

2.删除数据

// 删除选定的项
listBox1.Items.Remove(listBox1.SelectedItem);
 
// 或者删除第n项
listBox1.Items.RemoveAt(n);

3.清空数据

listBox1.Items.Clear();

4.选择项

// 选择第n项
listBox1.SelectedIndex = n;
 
// 或者选择包含特定文本的项
for (int i = 0; i < listBox1.Items.Count; i++)
{
    if (listBox1.Items[i].ToString() == "item4")
    {
        listBox1.SelectedIndex = i;
        break;
    }
}

5.排序

//listBox1排序);
listBox1.Sorted = true;

6.获取选中的项

int selectedIndex = listBox1.SelectedIndex;

7.获取ListBox中的所有项

List<string> allItems = new List<string>();
foreach (string item in listBox1.Items)
{
    allItems.Add(item.ToString());
}

8.综合示例

// ListBox控件操作
using System.Diagnostics;
using System.Linq;
namespace _148_2
{
    public partial class Form1 : Form
    {
        private static ListBox? listBox1;
        private Button? button1;
        private static TextBox? textBox1;
        private Button? button2;
        private Button? button3;
        private Button? button4;
 
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Load += Form1_Load;
        }
 
        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // listBox1
            // 
            listBox1 = new ListBox
            {
                FormattingEnabled = true,
                ItemHeight = 17,
                Location = new Point(12, 12),
                Name = "listBox1",
                Size = new Size(270, 174),
                TabIndex = 1
            };
            // 
            // button1
            // 
            button1 = new Button
            {
                ForeColor = SystemColors.ActiveCaptionText,
                TabIndex = 2,
                Text = "操作",
                UseVisualStyleBackColor = true,
                Location = new Point(231, 221),
                Name = "button1",
                Size = new Size(50, 23)
            };
            button1.Click += Button1_Click;
            // 
            // textBox1
            // 
            textBox1 = new TextBox
            {
                Location = new Point(12, 192),
                Name = "textBox1",
                Size = new Size(270, 23),
                TabIndex = 3
            };
            // 
            // button2
            // 
            button2 = new Button
            {
                ForeColor = SystemColors.ActiveCaptionText,
                TabIndex = 4,
                Text = "清空",
                UseVisualStyleBackColor = true,
                Location = new Point(166, 221),
                Name = "button2",
                Size = new Size(49, 23)
            };
            button2.Click += Button2_Click;
            // 
            // button3
            // 
            button3 = new Button
            {
                ForeColor = SystemColors.ActiveCaptionText,
                Location = new Point(12, 221),
                Name = "button3",
                Size = new Size(75, 23),
                TabIndex = 5,
                Text = "复制全部",
                UseVisualStyleBackColor = true
            };
            button3.Click += Button3_Click;
            // 
            // button4
            // 
            button4 = new Button
            {
                ForeColor = SystemColors.ActiveCaptionText,
                Location = new Point(103, 221),
                Name = "button4",
                Size = new Size(47, 23),
                TabIndex = 6,
                Text = "删除",
                UseVisualStyleBackColor = true
            };
            button4.Click += Button4_Click;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(294, 255);
            Controls.Add(button4);
            Controls.Add(button3);
            Controls.Add(button2);
            Controls.Add(textBox1);
            Controls.Add(button1);
            Controls.Add(listBox1);
            ForeColor = SystemColors.ControlLightLight;
            Name = "Form1";
            Text = "ListBox操作";
        }
 
        private void Button1_Click(object? sender, EventArgs e)
        {
            ListBoxOperations();
        }
 
        private static void ListBoxOperations()
        {
            // 创建一个字符串数组
            string[] items = ["item3", "item2", "item1"];
 
            // 添加字符串数组到ListBox
            listBox1!.Items.AddRange(items);
 
            // 添加单个字符串到ListBox
            listBox1.Items.Add("item4");
 
            //listBox1排序
            listBox1.Sorted = true;
 
            // 选择第2个项(索引从0开始)
            listBox1.SelectedIndex = 1;
 
            // 获取选中的项
            string selectedValue = listBox1.SelectedItem!.ToString()!;
            textBox1!.Text = "Selected Value: " + selectedValue;
 
            // 获取选中的项的索引
            int selectedIndex = listBox1.SelectedIndex;
            textBox1!.Text += "  Selected Index: " + selectedIndex;
        }
        // 清空所有
        private void Button2_Click(object? sender, EventArgs e)
        {
            listBox1!.Items.Clear();
        }
        // 复制并添加全部
        private void Button3_Click(object? sender, EventArgs e)
        {
            List<string> allItems = [];
            foreach (string item in listBox1!.Items)
            {
                allItems.Add(item.ToString());
            }
            foreach (string item in allItems)
            {
                listBox1.Items.Add(item);
            }
        }
        // 删除选中
        private void Button4_Click(object? sender, EventArgs e)
        {
            listBox1!.Items.Remove(listBox1.SelectedItem!);
        }
    }
}

结果如下

到此这篇关于详解C#如何对ListBox控件中的数据进行操作的文章就介绍到这了,更多相关C#操作ListBox数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C# MVC 使用LayUI实现下拉框二级联动的功能

    C# MVC 使用LayUI实现下拉框二级联动的功能

    这篇文章主要介绍了C# MVC 如何使用LayUI实现下拉框二级联动,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
    2020-06-06
  • c#图片处理之图片裁剪成不规则图形

    c#图片处理之图片裁剪成不规则图形

    最近项目要求实现不规则裁剪功能。本来想用html5的canvas在前端实现的,但是发现有点困难,以下为C#端对图对片的处理
    2014-05-05
  • C#中的 Dictionary常用操作

    C#中的 Dictionary常用操作

    C#中的Dictionary<TKey,TValue>是用于存储键值对集合的泛型类,允许通过键快速检索值,并且具有唯一键、动态大小和无序集合的特性,常用操作包括添加、访问、修改、删除元素,以及检查键或值是否存在,本文介绍C#中的 Dictionary常用操作,感兴趣的朋友一起看看吧
    2025-03-03
  • Entity Framework主从表的增删改

    Entity Framework主从表的增删改

    这篇文章介绍了Entity Framework主从表的增删改,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C# 中的 IReadOnlyDictionary 和 IReadOnlyList实例详解

    C# 中的 IReadOnlyDictionary 和 IReadOnlyLis

    C# 中的IReadOnlyDictionary和IReadOnlyList是接口,用于表示只读的字典和只读的列表,这些接口提供了对集合的只读访问权限,即不允许对集合进行修改操作,这篇文章主要介绍了C# 中的 IReadOnlyDictionary 和 IReadOnlyList实例详解,需要的朋友可以参考下
    2024-03-03
  • C# 数独求解算法的实现

    C# 数独求解算法的实现

    这篇文章主要介绍了C# 数独求解算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • 10分钟学会VS NuGet包私有化部署

    10分钟学会VS NuGet包私有化部署

    本文主要介绍了10分钟学会VS NuGet包私有化部署,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • C# Socket的TCP通讯的实例代码

    C# Socket的TCP通讯的实例代码

    本篇文章主要介绍了C# Socket的TCP通讯,socket通讯方式有两种:同步和异步,详细的介绍了这两种方法,有兴趣的可以了解一下。
    2016-12-12
  • C#使用WebService结合jQuery实现无刷新翻页的方法

    C#使用WebService结合jQuery实现无刷新翻页的方法

    这篇文章主要介绍了C#使用WebService结合jQuery实现无刷新翻页的方法,涉及C#中WebService与jQuery操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • C#利用iTextSharp组件给PDF文档添加图片/文字水印

    C#利用iTextSharp组件给PDF文档添加图片/文字水印

    这篇文章主要给大家介绍了关于如何C#利用iTextSharp组件给PDF文档添加图片/文字水印的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10

最新评论