Blink all the Labels when Color is Red using C# and VB.Net in Windows Application

Last Reply 7 months ago By dharmendr

Posted 7 months ago

blink all the label when label color is red in c# windows application

Posted 7 months ago

Hi anirudhp,

Check this example. Now please take its reference and correct your code.

I have added Five Label controls in the form and set the BackColor of Color label to red for checking.

Code

C#

private void Form1_Load(object sender, EventArgs e)
{
    lblColor.BackColor = Color.Red;
    System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
    if (lblColor.BackColor == Color.Red)
    {
        timer1.Start();
        System.Threading.Thread thread = new System.Threading.Thread(Blink);
        thread.Start();
    }
}

int count = 10;
private void Blink(object o)
{
    bool go = false;
    while (count > 0)
    {
        while (!go)
        {
            lblId.BackColor = Color.Red;
            lblName.BackColor = Color.Red;
            lblCity.BackColor = Color.Red;
            lblCountry.BackColor = Color.Red;
            go = true;
            System.Threading.Thread.Sleep(1000);
        }

        while (go)
        {
            lblId.BackColor = Color.Green;

            lblName.BackColor = Color.Green;
            lblCity.BackColor = Color.Green;
            lblCountry.BackColor = Color.Green;
            go = false;
            System.Threading.Thread.Sleep(1000);
        }
    }
}

VB.Net

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    lblColor.BackColor = Color.Red
    Dim timer1 As System.Windows.Forms.Timer = New System.Windows.Forms.Timer()
    If lblColor.BackColor = Color.Red Then
        timer1.Start()
        Dim thread As System.Threading.Thread = New System.Threading.Thread(AddressOf Blink)
        thread.Start()
    End If
End Sub

Private count As Integer = 10

Private Sub Blink(ByVal o As Object)
    Dim go As Boolean = False
    While count > 0
        While Not go
            lblId.BackColor = Color.Red
            lblName.BackColor = Color.Red
            lblCity.BackColor = Color.Red
            lblCountry.BackColor = Color.Red
            go = True
            System.Threading.Thread.Sleep(1000)
        End While
        While go
            lblId.BackColor = Color.Green
            lblName.BackColor = Color.Green
            lblCity.BackColor = Color.Green
            lblCountry.BackColor = Color.Green
            go = False
            System.Threading.Thread.Sleep(1000)
        End While
    End While
End Sub

Screenshot


Posted 7 months ago

Hi anirudhp,

Check this example. Now please take its reference and correct your code.

C#

private void Form1_Load(object sender, EventArgs e)
{
    lblId.ForeColor = Color.Red;
    lblCountry.ForeColor = Color.Red;
    System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
    if (lblId.ForeColor == Color.Red)
    {
        timer1.Start();
        System.Threading.Thread thread = new System.Threading.Thread(BlinkId);
        thread.Start();
    }
    if (lblName.ForeColor == Color.Red)
    {
        timer1.Start();
        System.Threading.Thread thread = new System.Threading.Thread(BlinkName);
        thread.Start();
    }
    if (lblCity.ForeColor == Color.Red)
    {
        timer1.Start();
        System.Threading.Thread thread = new System.Threading.Thread(BlinkCity);
        thread.Start();
    }
    if (lblCountry.ForeColor == Color.Red)
    {
        timer1.Start();
        System.Threading.Thread thread = new System.Threading.Thread(BlinkCountry);
        thread.Start();
    }
}

int count = 10;
private void BlinkId(object o)
{
    bool go = false;
    while (count > 0)
    {
        while (!go)
        {
            lblId.BackColor = Color.White;
            go = true;
            System.Threading.Thread.Sleep(500);
        }

        while (go)
        {
            lblId.BackColor = Color.Green;
            go = false;
            System.Threading.Thread.Sleep(500);
        }
    }
}
private void BlinkName(object o)
{
    bool go = false;
    while (count > 0)
    {
        while (!go)
        {
            lblName.BackColor = Color.White;
            go = true;
            System.Threading.Thread.Sleep(500);
        }

        while (go)
        {
            lblName.BackColor = Color.Green;
            go = false;
            System.Threading.Thread.Sleep(500);
        }
    }
}
private void BlinkCity(object o)
{
    bool go = false;
    while (count > 0)
    {
        while (!go)
        {
            lblCity.BackColor = Color.White;
            go = true;
            System.Threading.Thread.Sleep(500);
        }

        while (go)
        {
            lblCity.BackColor = Color.Green;
            go = false;
            System.Threading.Thread.Sleep(500);
        }
    }
}
private void BlinkCountry(object o)
{
    bool go = false;
    while (count > 0)
    {
        while (!go)
        {
            lblCountry.BackColor = Color.White;
            go = true;
            System.Threading.Thread.Sleep(500);
        }

        while (go)
        {
            lblCountry.BackColor = Color.Green;
            go = false;
            System.Threading.Thread.Sleep(500);
        }
    }
}

VB.Net

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    lblId.ForeColor = Color.Red
    lblCountry.ForeColor = Color.Red
    Dim timer1 As System.Windows.Forms.Timer = New System.Windows.Forms.Timer()
    If lblId.ForeColor = Color.Red Then
        timer1.Start()
        Dim thread As System.Threading.Thread = New System.Threading.Thread(AddressOf BlinkId)
        thread.Start()
    End If

    If lblName.ForeColor = Color.Red Then
        timer1.Start()
        Dim thread As System.Threading.Thread = New System.Threading.Thread(AddressOf BlinkName)
        thread.Start()
    End If

    If lblCity.ForeColor = Color.Red Then
        timer1.Start()
        Dim thread As System.Threading.Thread = New System.Threading.Thread(AddressOf BlinkCity)
        thread.Start()
    End If

    If lblCountry.ForeColor = Color.Red Then
        timer1.Start()
        Dim thread As System.Threading.Thread = New System.Threading.Thread(AddressOf BlinkCountry)
        thread.Start()
    End If
End Sub

Private count As Integer = 10

Private Sub BlinkId(ByVal o As Object)
    Dim go As Boolean = False
    While count > 0
        While Not go
            lblId.BackColor = Color.White
            go = True
            System.Threading.Thread.Sleep(500)
        End While
        While go
            lblId.BackColor = Color.Green
            go = False
            System.Threading.Thread.Sleep(500)
        End While
    End While
End Sub

Private Sub BlinkName(ByVal o As Object)
    Dim go As Boolean = False
    While count > 0
        While Not go
            lblName.BackColor = Color.White
            go = True
            System.Threading.Thread.Sleep(500)
        End While
        While go
            lblName.BackColor = Color.Green
            go = False
            System.Threading.Thread.Sleep(500)
        End While
    End While
End Sub

Private Sub BlinkCity(ByVal o As Object)
    Dim go As Boolean = False
    While count > 0
        While Not go
            lblCity.BackColor = Color.White
            go = True
            System.Threading.Thread.Sleep(500)
        End While
        While go
            lblCity.BackColor = Color.Green
            go = False
            System.Threading.Thread.Sleep(500)
        End While
    End While
End Sub

Private Sub BlinkCountry(ByVal o As Object)
    Dim go As Boolean = False
    While count > 0
        While Not go
            lblCountry.BackColor = Color.White
            go = True
            System.Threading.Thread.Sleep(500)
        End While
        While go
            lblCountry.BackColor = Color.Green
            go = False
            System.Threading.Thread.Sleep(500)
        End While
    End While
End Sub

Screenshot