【问题标题】:Showing what is displayed in ListBox in Alphabetical Order按字母顺序显示 ListBox 中显示的内容
【发布时间】:2015-11-27 19:05:30
【问题描述】:

http://imgur.com/f0aaiZN 抓取用户联系人,并随机显示它们,但我怎样才能让它按字母顺序显示,这样更容易找到某个。

这里有一些代码,不知道有没有需要。

    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);
        }
    }

【问题讨论】:

  • 您可以简单地使用ListBoxSorted 属性。你也可以使用listBox1.DataSource= Contacts.OrderBy(x =&gt; x).ToList()

标签: c# winforms


【解决方案1】:

要按字母顺序对项目进行排序,您只需将ListBoxSorted 属性设置为true。

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

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

【讨论】:

    【解决方案2】:

    一种方法是在绑定之前对联系人进行排序:

    listBox1.DataSource = Contacts.OrderBy( x =&gt; x ).ToList();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多