3

PointFタイプのOffsetというプロパティを持つカスタムコントロールがあります。

フォームデザイナからプロパティを編集できるようにしたい(現在は無効になっています)。

System.Drawing.Design.UITypeEditorから派生したクラスを指すEditorAttributeを追加する必要があることを読みました。

UITypeEditorから派生した組み込み型がすでにかなりの数あるようです。

System.ComponentModel.Design.BinaryEditor
System.ComponentModel.Design.CollectionEditor
System.ComponentModel.Design.DateTimeEditor
System.ComponentModel.Design.MultilineStringEditor
System.ComponentModel.Design.ObjectSelectorEditor
System.Drawing.Design.ColorEditor
System.Drawing.Design.ContentAlignmentEditor
System.Drawing.Design.CursorEditor
System.Drawing.Design.FontEditor
System.Drawing.Design.FontNameEditor
System.Drawing.Design.IconEditor

...など

PointFまたはPointタイプを編集するためのものが見つかりません。.NETはデザイナで常にPoint/PointFタイプを公開しているため、この機能はすでに存在しているようです。

車輪の再発明をする必要がないことを願っています-組み込みのUITypeEditorタイプがすでに存在する場合は、それを使用したいと思います。

ありがとう。

4

1 に答える 1

3

Point次のコードを使用して、コントロールのプロパティグリッドで編集できるタイプのカスタムコントロールにプロパティを追加できます。

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
 EditorBrowsable(EditorBrowsableState.Advanced),
 TypeConverter(typeof(PointConverter))]
public Point MyPointProperty { get; set; }

で同じ種類のアプローチを試してみると、 .NETに.NETがSizeF組み込まれていないことがわかります。自分で作成することもできますが、ここで見つけました(そして、以下のコードのほとんどをコピーして貼り付けました)。TypeConverterPointF

を使用すると、プロパティウィンドウで編集可能なPointF TypeConverterタイプのプロパティを記述できます。PointF

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
 EditorBrowsable(EditorBrowsableState.Advanced),
 TypeConverter(typeof(PointFConverter))]
public PointF MyPointFProperty { get; set; }

上記のリンク先の記事にあるPointFタイプのコンバーターコードは次のとおりです。

/// <summary>
/// PointFConverter
/// </summary>
public class PointFConverter : ExpandableObjectConverter
{
    /// <summary>
    /// Creates a new instance of PointFConverter
    /// </summary>
    public PointFConverter() {
    }

    /// <summary>
    /// Boolean, true if the source type is a string
    /// </summary>
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
        if (sourceType == typeof(string)) return true;
        return base.CanConvertFrom(context, sourceType);
    }

    /// <summary>
    /// Converts the specified string into a PointF
    /// </summary>
    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
        if (value is string) {
            try {
                string s = (string)value;
                string[] converterParts = s.Split(',');
                float x = 0;
                float y = 0;
                if (converterParts.Length > 1) {
                    x = float.Parse(converterParts[0].Trim());
                    y = float.Parse(converterParts[1].Trim());
                } else if (converterParts.Length == 1) {
                    x = float.Parse(converterParts[0].Trim());
                    y = 0;
                } else {
                    x = 0F;
                    y = 0F;
                }
                return new PointF(x, y);
            } catch {
                throw new ArgumentException("Cannot convert [" + value.ToString() + "] to pointF");
            }
        }
        return base.ConvertFrom(context, culture, value);
    }

    /// <summary>
    /// Converts the PointF into a string
    /// </summary>
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
        if (destinationType == typeof(string)) {
            if (value.GetType() == typeof(PointF)) {
                PointF pt = (PointF)value;
                return string.Format("{0}, {1}", pt.X, pt.Y);
            }
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}
于 2012-09-26T06:26:18.677 に答える