Windows form : Cannot set a new value for a property in design mode from a custom selection form

83 views Asked by At

I created a user control in .NET framework/C# that has a property called User from a type UserModel. UserModel is a simple class with a string for the name and an integer for the age.

Code of UserModel class :

[Serializable]
[ComVisible(true)]
[TypeConverter(typeof(UserTypeConverter))]
[Editor(typeof(UserTypeEditor), typeof(UITypeEditor))]

public class UserModel
{
    public string Name { get; set; }
    public int Age { get; set; }

    public UserModel(string name, int age) 
    {
        Name = name;
        Age = age;
    }

    public override string ToString() { return Name; }

}

code of the user control :

public partial class UserManagement: UserControl
{
    private UserModel _userModel = new UserModel("DefaultName", 0);

    [Category("Behavior"), Description("Instance of a user model")]
    public UserModel User
    {
        get
        { 
            return _userModel;
        }
        set
        {
            _userModel = value;
        }
    }                

    public UserManagement()
    {
        InitializeComponent();
    }
}

When I want to set new values for my User property. I open a new form with a custom type editor and a custom type selector to select the name and the age of the user. Here is the form open :

User selection form

Everything is working until now. I want my value to be saved as this.userManagement1.User = new UserModel("newName","newAge"); in the designer.cs of my form. But when I click on OK to valid my modifications I can see my new values in the return value of my form/type selector but my setter method of the property is never called. Which means my new value is not saved in the designer.cs file . So when I close and reopen my form the value is not saved.

If you want to test a project, here is a link to download the solution : link to the project

Has everyone ever experienced something like that and know how to solve this problem.

PS : This project is just to show a simple representation of what happened in a more complex project

0

There are 0 answers