如何在VS设计器中的自定义ColorEditor中显示Color.Name?

huangapple go评论81阅读模式
英文:

How to Display the Color.Name in a custom ColorEditor in VS Designer?

问题

我正在尝试按照这个代码来在VS Designer ColorEditor中添加一个新的选项卡。然而,下拉菜单显示了颜色的ARGB值,如下所示,这使得处理相似颜色变得困难。

如何编辑上面链接中的代码,以使ColorEditor显示ColorName的字符串属性?我尝试了各种方法,比如交换字典中的顺序。

我自上周以来一直在搜索如何做到这一点,我偶然发现了这个。我也尝试了这个,但这会创建一个扩展提供程序而不是一个选项卡。在某个时候,我曾考虑使用这个而不是ColorEditor,但这个不显示迷你颜色预览框。

在第一个示例中,我将键值对的顺序更改为<string, int>。我还尝试使用一个自定义类CustomColor,其中包含以下属性:

public class CustomColor {
    Color Color;
    string Name;
}

我还尝试将第二个链接中的被接受的答案合并到一起,但它不显示颜色的预览框,也不使用ColorEditor,而是使用了一个ExtenderProvider。具体来说,我尝试在CustomColor.cs中实现Find方法,并与CustomColorTypeConverter.cs一起使用,但不知道如何访问设计器中的下拉菜单。

希望您理解我尝试表达的内容,如果有语法错误,请原谅,因为英语不是我的母语。

英文:

I am trying to follow this code that adds a new tab in the VS Designer ColorEditor. However, the dropdown shows the ARGB of the colors as shown below, making it hard to work with colors that are kind of similar.

如何在VS设计器中的自定义ColorEditor中显示Color.Name?

如何在VS设计器中的自定义ColorEditor中显示Color.Name? 如何在VS设计器中的自定义ColorEditor中显示Color.Name?

How can I edit the code from the above link to make the ColorEditor display the ColorName's string property? I tried various methods, such as swapping the order in the dictionary.

I've also been searching since last week on how to do this, and I came across this. I also tried this, but this creates an extender provider instead of a tab. At some point, I did consider using this instead of the ColorEditor, but this does not show the mini Color Preview Box.

In the first example, swapped the order of the key-value pair into &lt;string, int&gt;. I also tried using a custom class CustomColor that has the following properties:

public class CustomColor {
    Color Color;
    string Name;
}

I also tried incorporating the accepted answer in the second link, but it does not show a preview box of the color, and it does not use the ColorEditor, but an ExtenderProvider. Specifically, I tried to implement the Find method in CustomColor.cs with the CustomColorTypeConverter.cs, but didn't know how to access the dropdown in the designer.

I hope you understand what I am trying to say, I apologize for any grammar mistakes as English is not my first language.

答案1

得分: 1

你只需要一个自定义颜色转换器来处理将主题颜色名称转换为相应的值,以及将颜色值转换为主题颜色名称。

请使用我在链接帖子中提到的相同的CustomColorEditor,然后创建以下转换器:

public class CustomColorConverter : ColorConverter
{
    Dictionary<int, string> colorNames = new Dictionary<int, string>
    {
        {Color.Red.ToArgb(), "Blood"},
        {Color.Green.ToArgb(), "Life potion"},
        {Color.Blue.ToArgb(), "Water"},
    };
    public override object ConvertTo(ITypeDescriptorContext context,
        CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string) &&
            value is Color &&
            colorNames.ContainsKey(((Color)value).ToArgb()))
            return colorNames[((Color)value).ToArgb()];
        return base.ConvertTo(context, culture, value, destinationType);
    }
    public override object ConvertFrom(ITypeDescriptorContext context,
        CultureInfo culture, object value)
    {
        if (value is string)
        {
            foreach (var item in colorNames)
            {
                if (string.Equals(item.Value, (string)value,
                    StringComparison.OrdinalIgnoreCase))
                    return Color.FromArgb(item.Key);
            }
        }
        return base.ConvertFrom(context, culture, value);
    }
}

然后注册转换器:

public class MyControl : Control
{
    [Editor(typeof(CustomColorEditor), typeof(UITypeEditor))]
    [TypeConverter(typeof(CustomColorConverter))]
    public Color MyColor { get; set; }
}

关闭所有设计器窗口并重新构建解决方案。然后,如果在设计模式下打开您的窗体并尝试操作MyControlMyColor属性,您将注意到当您从主题颜色中选择Water时,属性网格中会显示Water作为文本;或者如果您在属性网格中写入Blood,它会选择Blood颜色。

注意

您当然可以改进功能和代码。例如:

  • 您可以将所有命名颜色放在一个类中,如ThemeColor,然后在编辑器和转换器中使用它。
  • 如果选择了主题颜色,您可以在打开下拉菜单时选择主题选项卡。
英文:

You just need a custom color converter to take care of converting your theme color names to corresponding value, and the color values to your theme color name.

Use the same CustomColorEditor from my answer in the linked post, then create the following converter:

public class CustomColorConverter : ColorConverter
{
    Dictionary&lt;int, string&gt; colorNames = new Dictionary&lt;int, string&gt;
    {
        {Color.Red.ToArgb(), &quot;Blood&quot;},
        {Color.Green.ToArgb(), &quot;Life potion&quot;},
        {Color.Blue.ToArgb(), &quot;Water&quot;},
    };
    public override object ConvertTo(ITypeDescriptorContext context,
        CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string) &amp;&amp;
            value is Color &amp;&amp;
            colorNames.ContainsKey(((Color)value).ToArgb()))
            return colorNames[((Color)value).ToArgb()];
        return base.ConvertTo(context, culture, value, destinationType);
    }
    public override object ConvertFrom(ITypeDescriptorContext context,
        CultureInfo culture, object value)
    {
        if (value is string)
        {
            foreach (var item in colorNames)
            {
                if (string.Equals(item.Value, (string)value,
                    StringComparison.OrdinalIgnoreCase))
                    return Color.FromArgb(item.Key);
            }
        }
        return base.ConvertFrom(context, culture, value);
    }
}

Then register the converter:

public class MyControl : Control
{
    [Editor(typeof(CustomColorEditor), typeof(UITypeEditor))]
    [TypeConverter(typeof(CustomColorConverter))]
    public Color MyColor { get; set; }
}

Close all designer windows and rebuild the solution. Then if you open your form in design mode, and try to play with MyColor property of MyControl you will notice that when you select Water from the theme colors, it shows Water as the text in property grid; or if you write Blood in the property grid, it selects the Blood color.

如何在VS设计器中的自定义ColorEditor中显示Color.Name?

Note

You can of course improve the functionality and the code. For example:

  • You can put all your named color in a class like ThemeColor, then use it in the editor and the converter

  • You can select the theme tab when opening the dropdown if a theme color is selected.

huangapple
  • 本文由 发表于 2023年6月29日 17:41:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定