Solved

Toggle text color in C#

Posted on 2007-03-20
5
1,428 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

615 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