按字母顺序显示 ListBox 中显示的内容 [英] Showing what is displayed in ListBox in Alphabetical Order

查看:31
本文介绍了按字母顺序显示 ListBox 中显示的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://imgur.com/f0aaiZN抓取用户联系人,随机显示,不过如何让按字母顺序显示,方便查找.

http://imgur.com/f0aaiZN Grabs users contacts, and randomly displays them, how can I make it show alphabetically though, so its easier to find a certain one.

这里有一些代码,不知道有没有人需要它.

Heres some code, dont know if any would need it.

    private void Form1_Load(object sender, EventArgs e)
    {
        //I entered a message box so it doesn't crash instantly.
        MessageBox.Show("Please allow SkypeBot.vshost.exe to access skype if you haven't yet. (Look at your Skype application)");
        Skype skype = new Skype();
        skype.Attach();
        getContacts(skype);
    }

    List<string> Contacts = new List<string>();

    public void getContacts(Skype skype)
    {
        //This goes through all the contacts in the contacts list
        for (int i = 0; i < skype.HardwiredGroups.Count; i++)
        {
            //This checks if the user is a friend or not
            if (skype.HardwiredGroups[i + 1].Type == TGroupType.grpAllFriends)
            {
                //If they are, then do this loop
                for (int j = skype.HardwiredGroups[i + 1].Users.Count; j > 0; j--)
                {
                    //This adds the contact to the Contacts list we declared before.
                    Contacts.Add(skype.HardwiredGroups[i + 1].Users[j].Handle);
                }
            }
        }
        //This makes the listBox show the contents of the Contact list.
        listBox1.DataSource = Contacts;
    }
        //aot stands for amount of times.
    public void sendMessage(string message, string user, int aot, Skype skype)
    {
        for (int i = 0; i < aot; i++)
        {
            skype.SendMessage(user, message);
        }
    }

推荐答案

要按字母顺序对项目进行排序,您只需设置 SortedListBox 的属性设为 true.

To sort items alphabetically you can simply set Sorted property of ListBox to true.

同样使用 linq,您可以使用 OrderBy 升序或 OrderByDescending 方法以降序排序:

Also using linq, you can use OrderBy method to sort ascending or OrderByDescending method to to sort descending:

listBox1.DataSource= Contacts.OrderBy(x => x).ToList();

这篇关于按字母顺序显示 ListBox 中显示的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆