Search code examples
winformscolorstextboxfadeeffect

Windows Forms Textbox - fade-in/fade-out background color


I want to validate the user's input and I want to inform him/her about validation error with changing background color of a standard Windows Forms TextBox control.

But instead of changing color immediately I would like to use a color fading effect.

Is there any simple way to do it ?

Edit: I also have access to Infragistics controls, I'm not sure if it makes any difference.


Solution

  • Assuming this is C#/.NET, creating your own user control is an appropriate solution to this problem. Instead of inheriting from UserControl, your control should instead inherit from TextBox - this will make your control look and act just like an ordinary TextBox, and you can add code to handle the fading effect:

    public partial class MyCustomTextbox : Textbox
    {
    
    }
    

    To do the fading, you'd have to create some sort of timer to progressively change BackColor with a function like this:

    function FadeBackground(float progress)
    {
        Color color = Color.FromArgb(255, (int)((1 - progress) * 255),
            (int)((1 - progress) * 255));
        base.BackColor = color;
    }
    

    When parameter progress = 0, this will produce a white background, and when progress = 1 this will be full red.