RDR
RDR

Reputation: 1143

Showing what is displayed in ListBox in Alphabetical Order

https://i.sstatic.net/gurgQ.jpg 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);
        }
    }

Upvotes: 1

Views: 756

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125207

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

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

Upvotes: 3

Sievajet
Sievajet

Reputation: 3513

One way to do it, is sorting the contacts before binding it:

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

Upvotes: 3

Related Questions