1

I want to show a label for 3 seconds only and then disappear it. I am working on a WPF application.

public DispatcherTimer timer = new DispatcherTimer(); 
timer.Tick += new EventHandler(timer_Tick);

I started timer from the function

timer.Start(); 
    
private void timer_Tick(object sender, EventArgs e)
{
      /*
      if timer equals 3 seconds then 
      timer.stop();
      lblToast.Visibility = Visibility.Hidden;
      else
      lblToast.Visibility = Visibility.Visible;
      */
}

Is this the right way or is there any other easy way ?

2 Answers2

5

Using Wpf animation you can do this very easily.For animation visit this link

<Label Content="Hello World">
    <Label.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames BeginTime="0:0:0" 
                     Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0:0:3" 
                         Value="{x:Static Visibility.Collapsed}"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard> 
        </EventTrigger>
    </Label.Triggers>
</Label>
Heena
  • 8,450
  • 1
  • 22
  • 40
4

Set your Interval to 3000 and then just hide the label in the Tick event.

John Koerner
  • 37,428
  • 8
  • 84
  • 134