设计时的WinForms组件属性

时间:2017-04-19 18:32:19

标签: c# .net winforms components controls

我曾尝试编写自己的组件来更改某些控件属性。我的想法是将这个组件从工具箱中删除到表单中,在设计器的属性窗口中我可以选择应该使用哪个控件,如下所示:

Control properties in designer

我的代码是:

public class ShapeForm : Component
{
    private int _shape;

    public ShapeForm()
    {
        Shape = 20;
    }

    [TypeConverter(typeof(ControlTypeConverter))]
    public Control TargetControl { get; set; }

    [Browsable(false)] // don't show in the property grid 
    public List<Control> Controls { get; private set; }

    public ContainerControl Target { get; set; } = null;

    public override ISite Site
    {
        get { return base.Site; }
        set
        {
            base.Site = value;

            IDesignerHost host = value?.GetService(
                typeof(IDesignerHost)) as IDesignerHost;
            IComponent componentHost = host?.RootComponent;
            if (componentHost is ContainerControl)
            {
                Target = componentHost as ContainerControl;
                Controls = new List<Control>();
                foreach (Control control in (componentHost as ContainerControl).Controls)
                {

                    Controls.Add(control);
                }
                ;

            }
        }
    }

    public class ControlTypeConverter : TypeConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            // we only know how to convert from to a string
            return typeof(Control) == destinationType;
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value,
            Type destinationType)
        {
            if (typeof(string) == destinationType)
            {
                Control control = value as Control;
                if (control != null)
                    return control;
            }
            return "(none)";
        }
    }

    [
        Category("Shaping"),
        Description("Set corner shape radius"),
        Browsable(true)
    ]
    public int Shape
    {
        set
        {
            _shape = value;
            //SetShaping();
        }
        get { return _shape; }
    }


}'

但是我只得到一个元素,而不是像第一个截图所示的这种形式的所有控件:

[It shows only one ContainerControl and not Controls]

1 个答案:

答案 0 :(得分:0)

  

使用此代码;

B = sum( conjg(A)*A )