Solved

Howto sort a Word.DropDownListContentControl with Csharp

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 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