Solved

Howto sort a Word.DropDownListContentControl with Csharp

Posted on 2011-09-16
5
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS2010 Build fails to install 14 77
Amazon S3 .Net error 5 39
Include MS Database in Winforms Project using Intellilock 5 21
"The resource cannot be found" 1 22
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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