简体   繁体   中英

Reseting label back to design-time .Text value

I have a bunch of labels that I set their value in the designer and later during runtime update them, but after using them, I want to set them back to their default value. My intent with this is to reduce the amount of large code to help make it easier to read.

random example like, setting in the designer of lbl_fruit Text = no fruits available currently then

*code*
lbl_fruits.Text = "banana";
*code*
lbl_fruits.ResetText(); // I want something like this
lbl_fruits.Text = "no fruits available currently"; // Instead of this

The .ResetText(); doesn't work for this as the label text gets cleaned instead of returning to "no fruits available currently"

My current solution is making a custom label control.

public class ExLabel : Label
    {
        private string defaultValue = "";
        public string DefaultValue
        {
            get { return defaultValue; }
            set { defaultValue = value; this.Invalidate(); }
        }
        protected override void OnControlAdded(ControlEventArgs e)
        {
            defaultValue = this.Text;
            MessageBox.Show("This code is being run");
            base.OnControlAdded(e);
        }

        public void ResetValue()
        {
            this.Text = defaultValue;
        }
    }

This code currently solves my problem if I use the custom propriety I made, but for me the ideal solution would be to have the design-time text value as the default value and not an extra propriety I made. OnControlAdded() does not get executed, OnPaint() runs again when lbl_fruits.Text = "banana"; happens.

So the question is: Which event I can override so the code gets executed as soon as the label is loaded but doesn't run twice. And also, is there a simpler way of approaching this?

In the end the solution I used was this:

public class ExLabel : Label
{
    private string defaultValue = "";
    public string DefaultValue
    {
        get { return defaultValue; }
        set { defaultValue = value; this.Invalidate(); }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if(defaultValue == "" && !this.Text.Contains("exLabel"))
        {
            defaultValue = this.Text;
        }
        base.OnPaint(e);
    }

    public void ResetValue()
    {
        this.Text = defaultValue;
    }
}
public class ExLabel : Label
{
    private string defaultValue = "";
    public string DefaultValue
    {
        get { return defaultValue; }
        set { defaultValue = value; this.Invalidate(); }
    }
    protected override void OnControlAdded(ControlEventArgs e)
    {
        defaultValue = this.Text;
        MessageBox.Show("This code is being run");
        base.OnControlAdded(e);
    }

    public void ResetValue()
    {
        this.Text = defaultValue;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM