Solved

Howto sort a Word.DropDownListContentControl with Csharp

Posted on 2011-09-16
5
464 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
Comment Utility
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
Comment Utility
you can cast it to List of string and then short it and assign back
0
 
LVL 18

Expert Comment

by:Dennis Aries
Comment Utility
@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
Comment Utility
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
Comment Utility
I dint change anything and it works now under specific circumstances, but generally this is the solution
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hangfire / asp.net sample 3 27
How do I bind the results to a grid 3 22
Round a string to two digits 12 23
Long URL in SMS 6 22
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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