简体   繁体   English

如何将背景色设置为自定义文本框的原始颜色

[英]how set backcolor to the original color of the custom textbox

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!但不起作用!

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.当它停在该行时,hover 会覆盖现有的ForeColor以查看在更改之前调用的值。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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