Windows forms Property grid Collection Editor

kallzz 1 Reputation point
2022-05-18T13:13:59.86+00:00

I have a .NET windows forms property grid which is tied to the collection of entities

public class Entity
{

public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
}

It has a collectioneditor derived from CollectionEditor and attached to entities collection. The collection Editor works perfectly well when invoked from the property grid .

[Editor(typeof(EntityCollectionEditor), typeof(UITypeEditor))]
    public EntityCollection Entities
    {
        get { return entityCollection; }
    }

How can i invoke the propertygrid's instance Collection Editor on the click of a toolbar button with all the collectiondata?

The propertygrid instance is EntityPropertyGrid.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,198 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,097 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,031 Reputation points
    2022-05-18T14:57:12.593+00:00

    See if this is what you are looking for, replaced Hours with your model.

    To see where this came from see the following.

    internal class Editor : UITypeEditor
    {
        private IWindowsFormsEditorService _svc;
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            _svc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
    
            var listBox = new ListBox();
            var hours = new Hours();
            var data = hours.Range(ParentIncrement);
    
            foreach (var item in data)
            {
                listBox.Items.Add(item);
            }
    
            if (value != null)
            {
                listBox.SelectedItem = value;
            }
    
            _svc.DropDownControl(listBox);
    
            value = (string)listBox.SelectedItem;
    
            return value;
        }
    
    }
    
    0 comments No comments

  2. kallzz 1 Reputation point
    2022-05-18T18:41:16.767+00:00

    Thanks for the reply @Karen Payne MVP .

    I do have the editor attached to the property grid implemented similar to the code which you have shared and I can display the collection editor when clicked on the property grid control.

    The question is , how can i show the collection Editor from the property grid instance when clicked on a toolbar item ?


  3. Limitless Technology 39,336 Reputation points
    2022-05-25T15:22:36.35+00:00

    Hi Kallzz-7817,

    As per your need there is no official documented way or approach, but as we have Winforms button, so you can do anything with it. You'll need to use your own editor. But you can derive from the standard editor class too. Here is an example of such approach:-

    You can define the custom editor attribute on the collection property in following way :-

    [Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]
    public List<Child> Children { get; }
    With this editor code:

    // CollectionEditor needs a reference to System.Design.dll
    public class MyCollectionEditor : CollectionEditor
    {
    public MyCollectionEditor(Type type)
    : base(type)
    {
    }

    protected override CollectionForm CreateCollectionForm()
    {
        CollectionForm form = base.CreateCollectionForm();
        var addButton = (ButtonBase)form.Controls.Find("addButton", true).First();
        addButton. Click += (sender, e) =>
            {
                MessageBox. Show("hello world");
            };
        return form;
    }
    

    }

    Here the add button is a simple Winforms button, so you can do anything with it.


    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments