question

kallzz-7817 avatar image
0 Votes"
kallzz-7817 asked LimitlessTechnology-2700 answered

Windows forms Property grid Collection Editor

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.



dotnet-csharpwindows-11
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

kallzz-7817 avatar image
0 Votes"
kallzz-7817 answered karenpayneoregon commented

Thanks for the reply @karenpayneoregon.

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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

Please show a screenshot and point to what you mean by toolbar

0 Votes 0 ·
kallzz-7817 avatar image kallzz-7817 karenpayneoregon ·

To make it more clear. How can i show the collection Editor from the property grid instance when clicked on a button?

0 Votes 0 ·

Not much out there and for good reason but you can check out the following.


0 Votes 0 ·
LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.