Solved

Toggle text color in C#

Posted on 2007-03-20
5
1,427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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 86

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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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