Delay between two font style changes

I want to change the font style of the control for a short time. for example 2 chairs. I like:

label1.Font = new Font(label1.Font, label1.Font.Style | FontStyle.Bold);
for(int i=0,i<4000000,i++);
label1.Font = new Font(label1.Font, label1.Font.Style | FontStyle.Regular);

but that will not work. what is the problem?

+1
source share
4 answers

What about this extension function?

public static class LabelExtensions
{
    public static Label BlinkText(this Label label, int duration)
    {
        Timer timer = new Timer();

        timer.Interval = duration;
        timer.Tick += (sender, e) =>
            {
                timer.Stop();
                label.Font = new Font(label.Font, label.Font.Style ^ FontStyle.Bold);
            };

        label.Font = new Font(label.Font, label.Font.Style | FontStyle.Bold);
        timer.Start();

        return label;
    }
}

Another interesting question comes to my mind when writing this extension:
Does this lead to a memory leak?

+3
source

This is not a way to wait two seconds. Try it instead System.Threading.Thread.Sleep(2000).

As for your problem: when you say, “This doesn't work,” I assume that the font remains bold rather than returning to regular. Use this instead:

FontStyle style = label1.Font.Style;
label1.Font = new Font(label1.Font, style | FontStyle.Bold); 
System.Threading.Thread.Sleep(2000)
label1.Font = new Font(label1.Font, style); 

, , MSDN Enum.

, . . Timer.

0

, Sleep , " ".

a)
) , .

.Refresh() , ( ) .

0

.

.NET: best way to execute lambda in UI thread after delay?

I prefer to use delegates and BeginInvoke()function.

Option c is Timereasier to understand and does not need to access Control from another thread.

0
source

All Articles