Solved

Toggle text color in C#

Posted on 2007-03-20
5
1,424 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:pratikshahse
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:jef06
ID: 18757439
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;
}
}
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18757446
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;
        }
0
 

Author Comment

by:pratikshahse
ID: 18757995
how do I make it to stop after it has toggled couple of times?
0
 
LVL 5

Expert Comment

by:jef06
ID: 18758008
timer.Enbled = false;
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18759356
Right...just setup a counter and use the Enabled() property or the Stop() method:

        private int counter = 0;
        private System.Windows.Forms.Timer tmr;

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

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

        private void button1_Click_1(object sender, EventArgs e)
        {
            counter = 0;
            tmr.Start();
        }

        void tmr_Tick(object sender, EventArgs e)
        {
            Color tmp = label1.ForeColor;
            label1.ForeColor = (Color)label1.Tag;
            label1.Tag = tmp;
            counter = counter + 1;
            if (counter == 6)
            {
                tmr.Stop();
            }
        }
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question