how set backcolor to the original color of the custom textbox

Viewed 45

I'm trying to change the forecolor for the textbox on focus (custom control)

 protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            BackColor = this.Parent.BackColor;
            ForeColor = _OnFocusForeColor;
        }

but how to back to the original color of the forecolor?

I tried:

protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            BackColor = Color.WhiteSmoke;
            ForeColor = this.ForeColor;
}

but doesn't work!

1 Answers

A couple of ideas:

First option

Set a break point on the line where you change the color, and debug your solution. When it stops at that line, hover over the existing ForeColor to see what the value is called before it is changed. Now you can use that color when you set it back.

Second option

Before setting the new color, save the already existing value in a property which you can then access and reapply later:

protected override void OnEnter(EventArgs e)
{
    base.OnEnter(e);
    BackColor = this.Parent.BackColor;
    
    // New:
    PreviousColor = ForeColor;
    ForeColor = _OnFocusForeColor;
}

..and then later, obviously:

    ForeColor = PreviousColor;
Related