Solved

Howto sort a Word.DropDownListContentControl with Csharp

Posted on 2011-09-16
5
504 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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 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