Solved

Howto sort a Word.DropDownListContentControl with Csharp

Posted on 2011-09-16
5
478 Views
Last Modified: 2013-12-17
I'd like to sort the DropDownListContentControl.DropDownListEntries alphabetical.
What I got so far is listed below. the Problem is this seems not to work proper because i don't know the object type of the ListEntries.
Thanks for your help

   public class Sort
    {
        public static void SortDropDownList(DropDownListContentControl ddlList)
        {
            try
            {
                ArrayList arl = null;
                if (ddlList.DropDownListEntries.Count > 0)
                {
                    arl = new ArrayList(ddlList.DropDownListEntries.Count);
                    foreach (var li in ddlList.DropDownListEntries)
                    {
                        arl.Add(li);
                    }
                }
                try
                {
                    arl.Sort(new ListItemComparer());
                }
                catch { }
                ddlList.DropDownListEntries.Clear();
                for (int i = 0; i < arl.Count; i++)
                {
                    ddlList.DropDownListEntries.Add(arl[i].ToString());
                }
            }
            catch { }
        }
    }

    public class ListItemComparer : IComparer
    {
        #region IComparer Members
        public int Compare(object x, object y)
        {
            try
            {
                var lix = (var)x;
                var liy = (var)y;
                CaseInsensitiveComparer c = new CaseInsensitiveComparer();
                return c.Compare(lix.Text, liy.Text);
            }
            catch {
                return 1;
            }
            
        }
        #endregion
    }
}

Open in new window

0
Comment
Question by:ASInfotrack
  • 2
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
Dennis Aries earned 500 total points
ID: 36548978
The listentries are Microsoft.Office.Interop.Word.ContentControlListEntry (see MSDN and further)

The entry-members (again MSDN) has a Text-property that you can use.
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36549046
you can cast it to List of string and then short it and assign back
0
 
LVL 18

Expert Comment

by:Dennis Aries
ID: 36549064
@nishantcomp2512: You are correct, provided that strings have gone in. It is possible to add different Text and Value-values that will get lost if you simply cast it to a List of string. (see Add-method).
0
 

Author Comment

by:ASInfotrack
ID: 36549074
figured this out in the meantime, but the elements are still not in the correct order. It sorts them but in a random order.

The DropDown element is filled in the correct order but it then displays it in a complete other order.

this is where the dropdown first gets filled:
After lUsrs.Sort(); the order is correct, but the dropdown element shows them in a different order.

            if (_settings.bNoAD == true)
                return;
            int i = 0;
            lUsrs.Sort();
            Sort.SortDropDownList(dDownAuthor);
            foreach (ActiveDirectoryUser adUsr in lUsrs)
            {
                string domainUser = Regex.Replace(adUsr.UserName, ".*\\\\(.*)", "$1", RegexOptions.None);


                try
                {
                    dDownAuthor.DropDownListEntries.Add(domainUser, adUsr.UserName, i);

                }

                catch (Exception)
                {
                }
                ;

                i = ++i;
            }

Open in new window

0
 

Author Closing Comment

by:ASInfotrack
ID: 36585864
I dint change anything and it works now under specific circumstances, but generally this is the solution
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now