2

I've created a property grid which has a custom array value. When the users selects one of the drop downs I want it to display a form. My problem isn't that it doesn't work, it's not it's over active and shows the form about 6 times despite only being declared once. If I choose ShowDialog it displays the form twice and on attempting to close the second dialog it creates another two instances of the form. Below is the code I'm using. I can't figure out what is wrong.

//Property Grid Type
 internal class TransferConnectionPropertyConverter : StringConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }

        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(new string[] { "", NEW_CONN });
        }            
    }

//Property Grid Node
[Category("Connection"),
Description("Specifies the type of connection for this task.")]
[TypeConverter(typeof(TransferConnectionPropertyConverter))]
public string TransferProtocol
{
    get
    {
         if (stConnectionType == NEW_CONN)
         {
              ConnectionDetailsForm connDetails = new ConnectionDetailsForm();
              connDetails.Show();                        
         }
         return stConnectionType;
    }
    set
    {
         stConnectionType = value;
    }                                       
}
zeencat
  • 589
  • 1
  • 5
  • 26

1 Answers1

1

You need an editor for that, and you definitely don't want to be showing a form during the get property of a property, since that could be called many times during the life of the PropertyGrid.

Simple class (found from this example):

public class StringEditor : UITypeEditor {
  public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
    return UITypeEditorEditStyle.Modal;
  }

  public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
    IWindowsFormsEditorService svc = (IWindowsFormsEditorService)
      provider.GetService(typeof(IWindowsFormsEditorService));
    if (svc != null) {
      svc.ShowDialog(new ConnectionDetailsForm());
      // update etc
    }
    return value;
  }
}

Then you decorate your property for this editor (and note, I removed the convertor since your property is just a string, nothing to convert):

[Category("Connection")]
[Description("Specifies the type of connection for this task.")]
[Editor(typeof(StringEditor), typeof(UITypeEditor))]
public string TransferProtocol {
  get {
    return stConnectionType;
  }
  set {
    stConnectionType = value;
  }
}
Community
  • 1
  • 1
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • You were right. I was calling the form show in the wrong place, putting it in the get was the reason it was displaying multiple times. 'd'oh' - just wasn't thinking. I changed it and placed it inside the set which works as I expected. – zeencat Jul 09 '12 at 13:23