How to save changes to project time properties that were made programmatically?

I have a custom control in which I added the string property Id.

When the control is placed on the form, I want the constructor to set this to Guid.NewGuid().ToString(), but only if it has not been set before.

When I manually edit this property from the constructor, it adds a line of code to the file Designer.cs. How can I do this programmatically? In particular, how to do this from a user control?

+2
source share
2 answers

usercontrol, . "MyLabel", Label.

, MyLabel, :

public class MyLabel: Label
{
    public string ID { get; set; }

    protected override void OnCreateControl()
    {
        base.OnCreateControl();
        if (this.DesignMode && string.IsNullOrEmpty(this.ID))
        {
            this.ID = Guid.NewGuid().ToString();
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        this.Text = this.ID;
    }
}

, ID, , , . , , .

OnPaint - ID , .

+3

, Properties.Settings. . MSDN.

.
: string[]. -

string ID = "";
if (Properties.Settings.Default.IDs!=null && Properties.Settings.Default.IDs.Length>0) {
   ID = Properties.Settings.Default.IDs[0];
}
else {
   ID = "random";
   Properties.Settings.Default.IDs = new string[1];
   Properties.Settings.Default.IDs[0] = ID;
   Properties.Settings.Default.Save();
}

, , , , ( ).

-1

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


All Articles