Property set does not start when using UITypeEditor

I have a property grid, which, when a button is clicked for one of the properties, is filled with some fields. However, the property set does not start. I do not know why.

private OptoSigmaSettings dataToGet = new OptoSigmaSettings(); [Editor(typeof(OptoSetupFormEditor), typeof(UITypeEditor))] [TypeConverter(typeof(ExpandableObjectConverter))] [Category("Setup")] public OptoSigmaSettings DataToGet { get { return dataToGet; } set { MessageBox.Show("Im here"); //This isnt happening. dataToGet = value; } } [Serializable] public class OptoSigmaSettings { private int duration = 0; private string direction = "Positive"; private string functionToCall = "Home"; public string FunctionToCall { get { return functionToCall; } set { functionToCall = value; } } public int Duration { get { return duration; } set { duration = value; } } public string Direction { get { return direction; } set { direction = value; } } } public class OptoSetupFormEditor : UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value) { IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService; OptoSigmaSettings opto = value as OptoSigmaSettings; if (service != null && opto != null) { using (OptoSigmaSetup form = new OptoSigmaSetup()) { DialogResult result; result = service.ShowDialog(form); if (result == DialogResult.OK) { opto.Direction = form.Direction; opto.FunctionToCall = form.FunctionToCall; opto.Duration = form.Duration; } } } return opto; } } 

This is a WinForms application using a standard property grid.

+6
source share
3 answers

In the end, there was a solution:

  public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value) { IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService; OptoSigmaLinearSettings opto = value as OptoSigmaLinearSettings; opto = (OptoSigmaLinearSettings)value; if (opto == null) { opto = new OptoSigmaLinearSettings(); } if (service != null) { using (OptoSigmaLinearSetup form = new OptoSigmaLinearSetup(opto)) { DialogResult result; result = service.ShowDialog(form); if (result == DialogResult.OK) { opto = form.GeneralSettings; } } } return opto; } 
+1
source

The problem is that your editor returns the exact same link (you get opto and return opto). Therefore, even if you change some of the internal properties of opto, opto ref will not change. If you absolutely need to enter your accessory, inside EditValue create a new OptoSigmaSettings and change its properties so that your form returns. Note that I do not see in your code how the form is initialized with the contents of the existing opto.

PS: I just saw your comment above. Please note that if you do not initialize your DataToGet, then it is null, and therefore it works the first time (the null value is different from the value returned by your form).

Note 2: Marino is right in saying that even if your set is not called, the properties of your object are still updated (Direction, FunctionToCall and Duration).

+2
source

It has been a while since I used the grid property, but here are my 2cents.

Nothing sets the DataToGet property in the DataToGet subclass that you created.

In your code:

OptoSigmaSettings opto = value as OptoSigmaSettings;

It looks like there is no casting value for the DataToGet, and then its DataToGet property is set:

DataToGet opto = value as DataToGet; opto.DataToGet = MyObject;

0
source

Source: https://habr.com/ru/post/891743/


All Articles