Property set isn't being triggered with a UITypeEditor implemented
Asked Answered
E

3

6

I have a property grid that when a button is clicked for one of the properties, certain fields are filled in. However the property's set isn't being triggered. 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 app using the standard property grid.

Echt answered 30/6, 2011 at 16:52 Comment(2)
If the editor doesn't set the property, I think it reads the object, then manipulates on its reference. Your dataToSet should already have the changes reflected...Ashore
It is not updating it. When i take out the initialization of = new OptoSigmaSettings();, then it will get in the "set" portion of the function. After that... it wont. The returning of the function does not seem to be working as i need to.Echt
E
1

Here was the solution in the end:

 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;
    }
Echt answered 16/7, 2011 at 18:2 Comment(0)
P
2

The problem is that your editor returns exactly the same reference (you get opto and you return opto). So even if you modify some internal properties of opto, the opto ref does not change. If you absolutely need to go in your set accessor, inside EditValue create a new OptoSigmaSettings and modify its properties with what your form returns. Note that I don't see in your code how the form is initialized with the content of the existing opto.

PS: I just saw your comment above. Note that if you don't initialize your dataToGet, then it is null, and that's why it works a first time (null is different than the value returned by your form).

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

Pumice answered 30/6, 2011 at 20:26 Comment(0)
E
1

Here was the solution in the end:

 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;
    }
Echt answered 16/7, 2011 at 18:2 Comment(0)
M
0

Its been a while since I've used a propertygrid, but here's my 2cents.

Nothing here is setting the DataToGet property on the DataToGet subclass you have created.

In your code:

OptoSigmaSettings opto = value as OptoSigmaSettings;

What looks like is missing is casting value to DataToGet and then setting its DataToGet property:

DataToGet opto = value as DataToGet; opto.DataToGet=myobject;

Mild answered 30/6, 2011 at 17:15 Comment(2)
Well I do this: OptoSigmaSettings opto = value as OptoSigmaSettings; in the Edit value function of the OptoSetupFormEditor. Shouldnt I at least see the messagebox pop up?Echt
No. You are creating a local variable and assigning the local value of the variable to the cast of value. You are not accessing a property. Also, as I said before, you are casting value to optoSigmaSettings, which does not have the property, not DataToGet.Mild

© 2022 - 2024 — McMap. All rights reserved.