在WinForms中设置自定义CollectionEditor表单的位置

时间:2016-04-15 14:29:39

标签: c# winforms position windows-forms-designer collectioneditor

我为WinForms控件编写了一个自定义集合编辑器。它的核心代码如下:

internal class MyCollectionEditor : CollectionEditor
{
    public MyCollectionEditor(Type type) : base(type) { }

    protected override System.ComponentModel.Design.CollectionEditor.CollectionForm CreateCollectionForm()
    {
        System.ComponentModel.Design.CollectionEditor.CollectionForm myForm = base.CreateCollectionForm();

        #region Adjust the property grid

        PropertyGrid myPropGrid = GetPropertyGrid(myForm);
        if (myPropGrid != null)
        {
            myPropGrid.CommandsVisibleIfAvailable = true;
            myPropGrid.HelpVisible = true;
            myPropGrid.PropertySort = PropertySort.CategorizedAlphabetical;
        }

        #endregion

        return myForm;
    }
}

我需要为集合编辑器表单设置自定义大小和位置,但我找不到这样做的方法。似乎集合编辑器表单始终由VS定位到其默认位置。有办法做我需要的吗?

1 个答案:

答案 0 :(得分:0)

它尊重您为表单设置的StartPositionDesktopLocationSize

public class MyCollectionEditor : CollectionEditor
{
    public MyCollectionEditor() : base(typeof(Collection<Point>)) { }
    protected override CollectionForm CreateCollectionForm()
    {
        var form = base.CreateCollectionForm();
        // Other Settings
        // ...
        form.StartPosition = FormStartPosition.Manual;
        form.Size = new Size(900, 600);
        form.DesktopLocation = new Point(10, 10);
        return form;
    }
}

然后以这种方式装饰你的财产:

[Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]
public Collection<Point> MyPoints { get; set; }