簡體   English   中英

Winforms Designer中用於自定義控件的Format屬性

[英]Format property for custom control in winforms designer

我有一個具有以下屬性的自定義控件

public ulong Mask { get; set; }

當我使用控件時,屬性在編輯器中顯示為十進制數字。

在此處輸入圖片說明

有沒有辦法將此屬性值顯示為十六進制? 如果有一種方法可以將十六進制數分解為四位數,那會更好。 謝謝!

UInt64Converter類提供了您所需的大多數內容,因為它支持從十六進制格式進行的轉換。 所需ConvertTo就是重寫ConvertTo to方法以顯示為十六進制。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM