Search code examples
c#winformspositionwindows-forms-designercollectioneditor

Set position for custom CollectionEditor form in WinForms


I wrote a custom collection editor for a WinForms control. Its core code looks like this:

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

I need to set a custom size and location for the collection editor form, but I could not find a way to do that. It seems the collection editor form is always positioned by VS to its default location. Is there a way to do what I need?


Solution

  • It respects to the StartPosition, DesktopLocation and Size which you set for the form:

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

    Then decorate your property this way:

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