简体   繁体   English

C#属性网格属性选择多次显示表单

[英]C# Property Grid property selection displaying form multiple times

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. 我的问题不是它不起作用,不是它过于活跃,尽管只声明了一次,但仍显示了大约6次该表格。 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. 如果我选择ShowDialog,它将显示两次该窗体,并在尝试关闭第二个对话框时创建该窗体的另外两个实例。 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;
    }                                       
}

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. 为此,您需要一个编辑器,并且您绝对不希望在get propertyget property期间显示表单,因为在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;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM