2

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?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
TecMan
  • 2,743
  • 2
  • 30
  • 64

1 Answers1

1

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; }
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 1
    Off-topic, but since you are customizing collection editor, you also may find this post helpful: [How to enable Default Values for properties in a 'CollectionEditor' dialog](http://stackoverflow.com/questions/35517211/how-to-enable-default-values-for-properties-in-a-collectioneditor-dialog) – Reza Aghaei Apr 15 '16 at 16:26
  • I try to restore the form position using one property, DesktopBounds, but the for size is always increased with every assignment to the DesktopBounds property. Do you know why this happens? – TecMan Apr 18 '16 at 11:18
  • @TecMan You can also use `SetDesktopBounds` without any problem, but you should first set `StartPosition` to `FormStartPosition.Manual`. – Reza Aghaei Apr 19 '16 at 11:28
  • Sure I set `StartPosition` to `Manual` first. It seems, the VS designer does something special and the form is expanded a little bit every time after I change its size/position in CreateCollectionForm. However, I solved my problem by placing the code that restores the position in the Load event for myForm. – TecMan Apr 19 '16 at 14:15