2015-11-27 81 views
1

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

您可以簡單地使用'ListBox'的'Sorted'財產。你也可以使用'listBox1.DataSource = Contacts.OrderBy(x => x).ToList()' –

回答

3

排序的項目按字母順序,你可以簡單地設置的ListBoxSorted屬性爲true。

而且使用LINQ,你可以使用OrderBy方法按升序排序或OrderByDescending方法降序排序:

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

一種方式做到這一點,是有約束力之前排序聯繫人:

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