如何动态更改组合框项目顺序 [英] How to change combobox items order dynamically

查看:59
本文介绍了如何动态更改组合框项目顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示'a','b','c','d'的Combobox。

同样的Combobox我需要改变显示顺序,如'c','a ',''','b'。



提前致谢。



我有详细说明在我的代码下面: -



我需要在我的ComboBox中显示颜色但不是所有的dotnet颜色,我必须只显示我需要的颜色。代码如下



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); 
}
}
}
}
}





所以In我需要显示ComboBox的顺序是



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.

推荐答案

除非它是自然排序顺序,否则最好的方法是在列表或数组中对组合框中的项进行排序按照你想要的顺序然后清除组合框并将新的排序列表放回去,确保控件上的.sorted = false。



制作一个更容易建议如果我知道你如何确定排序顺序,是什么让c在你的新排序列表中排在第一位?



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?

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]);
        }
        }


你在做什么似乎太复杂了。如果包含所需顺序的词典实际上是硬编码的,那么所需要的只是正确顺序的颜色列表。例如:

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:
Color[] deviceColors = new Color[] { Color.OrangeRed, Color.Orange, Color.Yellow };
comboBox_BorderColor.DisplayMember = "Name";
comboBox_BorderColor.DataSource = deviceColors;



Alan。


Alan.


这篇关于如何动态更改组合框项目顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆