c# Custom UITypeEditor not firing after pressing Ok and Cancel

171 Views Asked by At

I have a Form that allows user to customized the Chart like changing Color, LegendText and others. I implemented a custom UIEditor below:

class MySeriesCollectionTypeEditor : UITypeEditor
{        
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }
    public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
    {
        IWindowsFormsEditorService svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
        if (svc != null)
        {
            Series[] oldSeriesCollection = new Series[] { };
            oldSeriesCollection = (Series[])value;

            using (MyChartSeriesEditor frm = new MyChartSeriesEditor((Series[])value))
            {
                if (svc.ShowDialog(frm) == DialogResult.OK)
                {
                    return frm.SeriesCollections;
                }
                else
                {
                    return oldSeriesCollection;
                }
            }
        }
        return value;
    }
}

On my MyChartSeriesEditor:

public Series[] SeriesCollections { get; set; }
public OptChartSeriesEditor(Series[] _seriesCollections)
{
   InitializeComponent();
   btnOK.DialogResult = DialogResult.OK;
   btnCancel.DialogResult = DialogResult.Cancel;
}

Then the property:

[Browsable(true)]
[Category("Series")]
[DisplayName("SeriesCollections")]
[Editor(typeof(MySeriesCollectionTypeEditor), typeof(UITypeEditor))]
public Series[] SeriesCollections
{
   get { return _SeriesCollections; }
   set
   {
     _SeriesCollections = value;
     MessageBox.Show("Test Message!");        
   }
}

When I click on the Ellipsis it shows the Form but when I clicked on either Ok or Cancel, the Form just closes and nothing happened.

1

There are 1 best solutions below

1
vadzim dvorak On

Try this approach

    public Series[] SeriesCollections { get; set; }
    public OptChartSeriesEditor(Series[] _seriesCollections)
    {
       InitializeComponent();
       btnOK.Click += (source, e) =>
       {
           //do some saving or other logic required after ok
           this.DialogResult = DialogResult.OK;
           this.Close();
       };
       btnCancel.Click += (source, e) => 
       {
           this.DialogResult = DialogResult.Cancel;
           this.Close();
       };
    }