Set position for custom CollectionEditor form in WinForms
Asked Answered
D

1

3

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?

Discontinue answered 15/4, 2016 at 14:29 Comment(1)
Older question about creating custom CollectionEditors: How do you create a custom collection editor form for use with the property grid?Garnish
S
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; }
She answered 15/4, 2016 at 16:10 Comment(4)
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' dialogShe
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?Discontinue
@TecMan You can also use SetDesktopBounds without any problem, but you should first set StartPosition to FormStartPosition.Manual.She
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.Discontinue

© 2022 - 2024 — McMap. All rights reserved.