Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I enable the user to edit a multidimensional collection?

This is usually a problem if you have a custom collection type MyCollection which itself can take items of type MyCollection.

This is a problem because a single instance of a UITypeEditor is used by the framework irrespective of how many types and instances it serves to edit. Which means you cannot start editing your MyCollection from within a MyCollection editor, since the editor is already open.

To work around this problem, you can provide a custom editor as follows:

using System.Drawing.Design;
using System.ComponentModel.Design;

public class CustomCollectionEditor : CollectionEditor 
{ 
  // The base class has its own version of this property 
  // cached CollectionForm 
  private CollectionForm collectionForm; 
  
  public CustomCollectionEditor(Type type) 
    : base(type) {}
 
  public override object EditValue( ITypeDescriptorContext context, 
    IServiceProvider provider, object value ) 
  { 
    if ( collectionForm != null &&  collectionForm.Visible ) 
    { 
      // If the CollectionForm is already visible, then create a new instance 
      // of the editor and delegate this call to it. 
      BarItemsCollectionEditor editor = 
        new BarItemsCollectionEditor( CollectionType ); 
      return editor.EditValue( context, provider, value ); 
    } 
    else
      return base.EditValue( context, provider, value ); 
  }

  protected override CollectionForm CreateCollectionForm() 
  { 
    // Cache the CollectionForm being used. 
    collectionForm = base.CreateCollectionForm(); 
    return collectionForm; 
  } 
}

Contributed from George Shepherd's Windows Forms FAQ