Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Combobox which displaying 'a', 'b', 'c', 'd'.
The same Combobox I need to change the display order like 'c', 'a', 'd', 'b'.

Thanks in advance.

I have detailed explain below my code :-

I need to display Colors in my ComboBox but not all dotnet colors, I have to display only my required colors. Code below

private void FillComboBoxBorderColor(MoeValueMCInfo mcInfo)
{
comboBox_BorderColor.Items.Clear();
float mcVersion = 0;
if (float.TryParse(mcInfo.Version.StringValue, out mcVersion))
{
if (mcInfo.Level.ToString() == "Enterprise" && mcVersion >= 4.0)
{
Dictionary<string, string> deviceSupportedColors = new Dictionary<string, string>();
deviceSupportedColors.Add("0", "OrangeRed");
deviceSupportedColors.Add("1", "Orange");
deviceSupportedColors.Add("2", "Yellow");

//List<string> deviceSupportedColors = new List<string> { "OrangeRed", "Orange", "Yellow", "LawnGreen", "MediumSpringGreen", "Aqua", "CadetBlue", "SlateBlue", "Violet", "Magenta", "MediumVioletRed", "Gray"};
ArrayList ColorList = new ArrayList();
Type colorType = typeof(System.Drawing.Color);
PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static |
BindingFlags.DeclaredOnly | BindingFlags.Public);
foreach (PropertyInfo c in propInfoList)
{
if (deviceSupportedColors.ContainsValue(c.Name))
{
/*int indextoInsert = Convert.ToInt32((from d in deviceSupportedColors
where d.Value == c.Name
select d.Key).FirstOrDefault()); */

//comboBox_BorderColor.Items.Insert(indextoInsert, c.Name);
this.comboBox_BorderColor.Items.Add(c.Name); 
}
}
}
}
}


So In that I need to display the ComboBox order would be
OrangeRed", "Orange", "Yellow", "LawnGreen", "MediumSpringGreen", "Aqua", "CadetBlue", "SlateBlue", "Violet", "Magenta", "MediumVioletRed", "Gray" 


I tried using index but I could not achieve it. (My code mentioned in the command line)

What I have tried:

I used Index but that could not able to achieve.
Posted
Updated 8-Jul-17 5:53am
v3
Comments
Maciej Los 8-Jul-17 9:47am    
Please, provide your code. Use "Improve question" widget.
vasanthkumarmk 8-Jul-17 10:11am    
I need to display in the order of 'OrangeRed', 'Orange', 'Yellow' (mentioned in the List comment line).

Unless it is a natural sort order the best way is to sort the items in the combobox in a list or array into the order you want then clear the combobox and put the new sorted list back, make sure .sorted=false on the control.

It would be easier to make a suggestion if I knew how you determine the sort order, what makes c come first in your new sorted list?

C#
private void Form1_Load(object sender, EventArgs e)
        {
            Dictionary<string, string> deviceSupportedColors = new Dictionary<string, string>();
            Dictionary<string, string> deviceSortedColors = new Dictionary<string, string>();

            deviceSupportedColors.Add( "OrangeRed", "0");
            deviceSupportedColors.Add( "Orange", "1");
            deviceSupportedColors.Add( "Yellow", "2");

                    //List<string> deviceSupportedColors = new List<string> { "OrangeRed", "Orange", "Yellow", "LawnGreen", "MediumSpringGreen", "Aqua", "CadetBlue", "SlateBlue", "Violet", "Magenta", "MediumVioletRed", "Gray"};
                    ArrayList ColorList = new ArrayList();
                    Type colorType = typeof(System.Drawing.Color);
                    PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static |
                    BindingFlags.DeclaredOnly | BindingFlags.Public);
                    foreach (PropertyInfo c in propInfoList)
                    {
                        if (deviceSupportedColors.ContainsKey(c.Name))
                        {
                        string keyvalue = " ";

                            deviceSupportedColors.TryGetValue(c.Name, out keyvalue);

                            deviceSortedColors.Add(keyvalue, c.Name);
                        }
                    }
            var list = deviceSortedColors.Keys.ToList();
            list.Sort();

            // Loop through keys.
            foreach (var key in list)
            {
                comboBox1.Items.Add(deviceSortedColors[key]);
        }
        }
 
Share this answer
 
v2
Comments
vasanthkumarmk 8-Jul-17 10:03am    
In my case I never clear my ComboBox, need to keep the same items in the ComboBox and need to changing the position of the items.
Michael_Davies 8-Jul-17 10:11am    
Perfectly clear,

Combobox does not provide a method to allow you to move items around, the list is either sorted if .sorted=true or the items appear in the order yo load them.

So you have to copy the list elsewhere, clear the combobox, sort the list as you wish then put them back into the combobox.
vasanthkumarmk 8-Jul-17 10:24am    
I have improved my question, could you please recheck and let me know the answer? Its more helpful
Michael_Davies 8-Jul-17 10:42am    
The solution still stands, once you have selected the colours you want you then need to sort them as the order you want is not natural, in your list Orange comes before OrangeRed in the propertyinfo as that is the natural sort order, to get the order you want in a combobox you will have to sort them, now your code is clearer I suggest you create a second dictionary and add the element from the first dictionary if it is found in the propertyinfo then iterate the new dictionary in key order to get the sort you want.
vasanthkumarmk 8-Jul-17 10:49am    
Can you pleaseee give small code snap for your recommended.
What you are doing seems to be far too complicated. If the Dictionary containing the required order is actually hard coded then all that is required is a list of colours in the correct order. For example:
C#
Color[] deviceColors = new Color[] { Color.OrangeRed, Color.Orange, Color.Yellow };
comboBox_BorderColor.DisplayMember = "Name";
comboBox_BorderColor.DataSource = deviceColors;

Alan.
 
Share this answer
 
Comments
Michael_Davies 8-Jul-17 13:13pm    
Quite agree, but if he wants to do it that way...
vasanthkumarmk 8-Jul-17 13:38pm    
Thanks Alan N & Michael_Davies both answers helps lot.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900