gpt4 book ai didi

c# - Winforms 设计器中自定义控件的格式属性

转载 作者:行者123 更新时间:2023-11-30 21:34:15 32 4
gpt4 key购买 nike

我有一个具有以下属性的自定义控件

public ulong Mask { get; set; }

当我使用该控件时,该属性在编辑器中显示为十进制数。

enter image description here

有没有办法将此属性值显示为十六进制?如果有办法将十六进制数分成四位数字组,那就更好了。谢谢!

最佳答案

UInt64Converter Class提供了您需要的大部分内容,因为它支持十六进制格式的转换。所需要做的就是重写 ConvertTo 方法以显示为十六进制。

public class UInt64HexConverter : UInt64Converter
{
private static Type typeUInt64 = typeof(UInt64);

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}

if (((destinationType == typeof(string)) && (value != null)) && typeUInt64.IsInstanceOfType(value))
{
UInt64 val = (UInt64)value;
return "0x" + val.ToString("X");
}

if (destinationType.IsPrimitive)
{
return Convert.ChangeType(value, destinationType, culture);
}
return base.ConvertTo(context, culture, value, destinationType);
}
}

示例用法:

class BitControl : Control
{
[TypeConverter(typeof(UInt64HexConverter))]
public ulong Mask { get; set; }
}

关于c# - Winforms 设计器中自定义控件的格式属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50469508/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com