bircastri
bircastri

Reputation: 153

C# How to blink TextBox

I would like my TextBoxto blink intermittently.

I have this method

private void abilitaAltroToken(int indiceRiga, Grid grid)
{
    UIElement element = grid.Children[indiceRiga];
    Label label = (Label)element;
    element = grid.Children[++indiceRiga];
    TextBox textBox = (TextBox)element;
    textBox.Background = Brushes.Blue;
    label.Background = Brushes.Blue;

    Thread.Sleep(1000);
    label.Background = Brushes.White;

    Thread.Sleep(1000);
    label.Background = Brushes.Blue;

    Thread.Sleep(1000);
    label.Background = Brushes.White;

    Thread.Sleep(1000);
    label.Background = Brushes.Blue;       
}

This code does not return an error but, the blinking doesn't occur.

Upvotes: 0

Views: 8044

Answers (4)

Roger Deep
Roger Deep

Reputation: 172

Double click on the timer toolbox or drag a timer to the form. Add the following code:

private void timer1_Tick(object sender, EventArgs e)
{
    if (tickcolor) txtAno.BackColor = Color.Red;
    if (!tickcolor) txtAno.BackColor = Color.White;
    tickcolor = !tickcolor;
}

private void txtAno_KeyPress(object sender, KeyPressEventArgs e)
{
    timer1.Stop();
    txtAno.BackColor = Color.White;
}

In the form_load event add this:

    timer1.Interval = 500;
    timer1.Start();

    txtAno.Focus();

Declare the variable: private bool tickcolor = true;

This will make the textbox txtAno to flash red and white.

Upvotes: 1

Suresh
Suresh

Reputation: 4149

First of all, you should not put Thread.Sleep in your Main (UI) thread code, that will put the UI thread to sleep, and you wouldn't see any changes happening on the UI.

Personally, I would use animations (that too in XAML) and Triggers/VisualStates to achieve what you are trying here.

However, as I don't have much visibility of your XAML, Here is the procedural code to achieve blinking of label:

var colorAnim = new ColorAnimationUsingKeyFrames()
{
    KeyFrames = new ColorKeyFrameCollection
            {
                new DiscreteColorKeyFrame(Colors.White, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5))),
                new DiscreteColorKeyFrame(Colors.Blue, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))),
                new DiscreteColorKeyFrame(Colors.White, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1.5))),
                new DiscreteColorKeyFrame(Colors.Blue, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2))),
            }
};

var storyBoard = new Storyboard();

storyBoard.Children.Add(colorAnim);
Storyboard.SetTarget(storyBoard, label);
Storyboard.SetTargetProperty(storyBoard, new PropertyPath("(Background).(SolidColorBrush.Color)"));

storyBoard.Begin();

Basically, I translated the following code snippet from your question:

Thread.Sleep(1000);  
label.Background = Brushes.White;

Thread.Sleep(1000);  
label.Background = Brushes.Blue;

Thread.Sleep(1000);  
label.Background = Brushes.White;

Thread.Sleep(1000);  
label.Background = Brushes.Blue;

Upvotes: 5

Uri Goren
Uri Goren

Reputation: 13682

Use a Timer Object,

Here's a sample code

private static System.Timers.Timer aTimer;
private static bool blinkFlag;
private Label label;

private void abilitaAltroToken(int indiceRiga,Grid grid)
{
        UIElement element = grid.Children[indiceRiga];
        label = (Label)element;
        element = grid.Children[++indiceRiga];
        TextBox textBox = (TextBox)element;
        textBox.Background = Brushes.Blue;
        label.Background = Brushes.Blue;

    aTimer = new System.Timers.Timer(1000);

    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

    // Set the Interval to 1 seconds (1000 milliseconds).
    aTimer.Interval = 1000;
    aTimer.Enabled = true;

}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
   label.Background = (blinkFlag?Brushes.Blue:Brushes.White);
   blinkFlag=!blinkFlag;
   aTimer.Interval = 1000;
   aTimer.Enabled = true;
}

Upvotes: 2

odyss-jii
odyss-jii

Reputation: 2699

You should not be sleeping in the "UI-thread". I suggest that you try using a DispatcherTimer instead. See the following link for more information: http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer%28v=vs.110%29.aspx

Upvotes: 1

Related Questions