Link to home
Start Free TrialLog in
Avatar of pratikshahse
pratikshahse

asked on

Toggle text color in C#

I want to toggle the color of the text for my label every 3 seconds. i want to toggle the color from black to red.
I am using C# (.NET 1.0). How can I do that.

THanks

Avatar of jef06
jef06
Flag of United States of America image

You can use a timer drop it in your form, and you do a modulo on 3 and if the color nlack chage it to red otherwise black

timer_Tick()
{
if((DateTime.Now.Second % 3) ==0)
{
if(textBox.BackColor == Color.Black)
textBox.BackColor == Color.Red;
else
textBox.BackColor == Color.Black;
}
}
Avatar of Mike Tomlinson
In C# WinForms...

        private System.Windows.Forms.Timer tmr;

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.ForeColor = Color.Red;
            label1.Tag = Color.Black;

            tmr = new System.Windows.Forms.Timer();
            tmr.Interval = 3000;
            tmr.Tick += new EventHandler(tmr_Tick);
            tmr.Start();
        }

        void tmr_Tick(object sender, EventArgs e)
        {
            Color tmp = label1.ForeColor;
            label1.ForeColor = (Color)label1.Tag;
            label1.Tag = tmp;
        }
Avatar of pratikshahse
pratikshahse

ASKER

how do I make it to stop after it has toggled couple of times?
timer.Enbled = false;
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial