Solved

Toggle text color in C#

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now