none
How to make label blinking RRS feed

  • Question

  • Have a label in the form.

    I want to make it blinking.

    How could I do this?

    (I'm using WinForm in VS2017)

    • Moved by CoolDadTx Friday, November 8, 2019 6:17 PM Winforms related
    Friday, November 8, 2019 3:17 PM

Answers

  • Have a label in the form.

    I want to make it blinking.

    How could I do this?

    As Castorix31 suggested, a timer can be used to trigger a change in the label's
    visibility. In the timer event you can toggle the visibility. Here's a *very*
    basic example to illustrate the effect.

    Create a new WinForms project. Put a label on the form.

    Double-click on the form in the designer to create a Form1_Load event handler
    block and add the code shown in this example.

    using System;
    using System.Windows.Forms;
    
    namespace CS_WinForms_blinking_label
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void TimerEventProcessor(Object myObject,
                                                    EventArgs myEventArgs)
            {
                if (label1.Visible) label1.Visible = false;
                else label1.Visible = true;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                label1.Text = "Hello Vietnam!";
    
                Timer timer = new Timer();
                timer.Tick += new EventHandler(TimerEventProcessor);
                timer.Interval = 500;
                timer.Start();                                         }
        }
    }
    

    - Wayne

    • Marked as answer by Jeff0803 Monday, December 2, 2019 2:35 PM
    Friday, November 8, 2019 4:39 PM
  • Hi Jeff0803,

    As Castorix31 and WayneAKing pointed, you can use a timer.

    Besides, you can use method await Task.Delay.

    Here is a code example you can refer to.

    private async void Blink()
    {
          while (true)
          {
           await Task.Delay(500);
           label1.Visible = label1.Visible ==false? true : false;
          }
    }
    private void Button1_Click(object sender, EventArgs e)
    {
           Blink();
    }

    Best Regards,

    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 11, 2019 6:36 AM

All replies

  • Just use a Timer
    Friday, November 8, 2019 3:34 PM
  • Have a label in the form.

    I want to make it blinking.

    How could I do this?

    As Castorix31 suggested, a timer can be used to trigger a change in the label's
    visibility. In the timer event you can toggle the visibility. Here's a *very*
    basic example to illustrate the effect.

    Create a new WinForms project. Put a label on the form.

    Double-click on the form in the designer to create a Form1_Load event handler
    block and add the code shown in this example.

    using System;
    using System.Windows.Forms;
    
    namespace CS_WinForms_blinking_label
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void TimerEventProcessor(Object myObject,
                                                    EventArgs myEventArgs)
            {
                if (label1.Visible) label1.Visible = false;
                else label1.Visible = true;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                label1.Text = "Hello Vietnam!";
    
                Timer timer = new Timer();
                timer.Tick += new EventHandler(TimerEventProcessor);
                timer.Interval = 500;
                timer.Start();                                         }
        }
    }
    

    - Wayne

    • Marked as answer by Jeff0803 Monday, December 2, 2019 2:35 PM
    Friday, November 8, 2019 4:39 PM
  • Hi Jeff0803,

    As Castorix31 and WayneAKing pointed, you can use a timer.

    Besides, you can use method await Task.Delay.

    Here is a code example you can refer to.

    private async void Blink()
    {
          while (true)
          {
           await Task.Delay(500);
           label1.Visible = label1.Visible ==false? true : false;
          }
    }
    private void Button1_Click(object sender, EventArgs e)
    {
           Blink();
    }

    Best Regards,

    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 11, 2019 6:36 AM