当前位置: 代码网 > it编程>编程语言>C# > 详解C#如何对ListBox控件中的数据进行操作

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

2024年05月18日 C# 我要评论
c#中对listbox控件中的数据进行的操作主要包括添加、删除、清空、选择、排序等。1.添加数据// 添加一个字符串数组listbox1.items.addrange(new string[] { "

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数据内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com