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

Set position for custom CollectionEditor form in WinForms

我为 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 定位到其默认位置。有什么办法可以满足我的需求吗?

它尊重您为表格设置的StartPosition, DesktopLocation and Size

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