1

I'm trying to show text in a label in wpf when a button is pressed and then hide after couple of seconds. I know there are answers of this, but my problem is different.

I used these 2 ways for hiding the label:

One

   //When the button is pressed
   label_plus.Visibility = System.Windows.Visibility.Visible;

   DispatcherTimer timer = new DispatcherTimer();
   timer.Interval = new TimeSpan(0, 0, 5);
   timer.Tick += timer_Tick;                //Or, timer.Tick += new EventHandler(timer_Tick);
   timer.Start();

// The timer event handler
void timer_Tick(object sender, EventArgs e)
        {
            label_plus.Visibility = System.Windows.Visibility.Collapsed;
}

Two

   //Button pressed
   label_plus.Content = label_plus1.Content = "+";
   DispatcherTimer timer = new DispatcherTimer();
   timer.Interval = new TimeSpan(0, 0, 5);
   timer.Tick += (o, args) => label_plus.Content = "";
   timer.Start();

Note: I the second one is almost same, except the "timer.tick += (o, args)" line. I got this code from: Here. It was a Form application code, so I just tried that part and it worked.

The 1st code I got directly from here.

The problem is this both works pretty well on 1st and second time. And maybe 3rd. But after that I feel like the timer second is decreasing. After 2/3 times, it hides within 3/4 seconds, after that it barely stays for 1 second or less.

Is there a better way to do this or getting rid of this problem? I'm new in Visual Studio.

Update: This also works well, but keeps repeating. Any way to stop after one process?

var timer = new System.Timers.Timer();

            timer.Elapsed += timer_Tick;
            timer.Interval = 3000;
            timer.Enabled = true;
            timer.Start();

void timer_Tick(object sender, EventArgs e)
        {
            //label_plus.Visibility = label_plus1.Visibility = System.Windows.Visibility.Collapsed; 
            MessageBox.Show("Show some data");

        }

Thanks in advance.

Community
  • 1
  • 1
  • 2
    As you observed, the issue is that the timer is periodic. Try `((Timer)sender).Enabled = false;` in the handler. Also, try creating only one timer and saving it, instead of creating new timers each time you want a short duration display. – Ben Voigt Jun 01 '15 at 21:57
  • Thanks, it also worked. –  Jun 01 '15 at 23:40

3 Answers3

6

This works:

XAML:

<StackPanel>
    <Button Width="50"
            Height="50"
            Click="Button_Click"
            Content="OK" />
    <Label x:Name="MyLabel"
            Content="THIS IS A LABEL"
            FontSize="30"
            Visibility="Collapsed" />
</StackPanel>

Codebehind:

private DispatcherTimer dispatcherTimer;

public MainWindow()
{
    InitializeComponent();

    //Create a timer with interval of 2 secs
    dispatcherTimer = new DispatcherTimer();
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    //Things which happen before the timer starts
    MyLabel.Visibility = System.Windows.Visibility.Visible;

    //Start the timer
    dispatcherTimer.Start(); 
}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    //Things which happen after 1 timer interval
    MessageBox.Show("Show some data");
    MyLabel.Visibility = System.Windows.Visibility.Collapsed;

    //Disable the timer
    dispatcherTimer.IsEnabled = false;
}
goobering
  • 1,547
  • 2
  • 10
  • 24
  • Thanks, it worked. And explanation was also helpful. –  Jun 01 '15 at 23:36
  • btw I had to put "private DispatcherTimer dispatcherTimer;" after main window, otherwise it doesn't work. –  Jun 09 '15 at 16:44
  • I've got that line after the class declaration (public partial class MainWindow : Window), but before the constructor shown above. It should be a global variable, so you can't put inside a method. – goobering Jun 09 '15 at 16:53
6

This will shows an "Error" label for 3 sec when you click on the button and the label will gets hide after 3 sec.

XAML

<Window.Resources>
    <Storyboard x:Key="sbHideAnimation" >
        <DoubleAnimation Storyboard.TargetProperty="Opacity"  From="1" To="1" Duration="0:0:3" /><!--label shows for 3 sec-->
        <DoubleAnimation Storyboard.TargetProperty="Opacity" BeginTime="0:0:3" From="1" To="0" DecelerationRatio=".5" Duration="0:0:2" /><!--Fade out the label after 3 sec-->
    </Storyboard>
</Window.Resources>
<Grid>
    <Label x:Name="lblError" Content="Error" FontSize="20" HorizontalAlignment="Center" Opacity="0"/>
    <Button Click="Button_Click" Content="Button" VerticalAlignment="Center" Width="100" />
</Grid>

Code behind(c#)

using System.Windows;
using System.Windows.Media.Animation;

namespace WpfApplication1
{    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {           
            Storyboard sb = Resources["sbHideAnimation"] as Storyboard;
            sb.Begin(lblError);
        }
    }
}

You can adjust the time duration for showing the label by changing the value of attributes 'BeginTime' and 'Duration' in the Storyboard "sbHideAnimation"

Output enter image description here

Deepu Reghunath
  • 8,132
  • 2
  • 38
  • 47
0

You could always approach it as a WPF animation. They tend to give better control over things like fading in and out as needed. There are a lot of different approaches to this problem, but S.L. gives a couple of good visibility animation examples here: Apply animation on WPF control visibility change

Community
  • 1
  • 1
BlindGarret
  • 155
  • 7
  • Thanks, but I think the programming approach will be much easier for me. As, I don't know about advanced codes in XAML. I have tried another with System.Timers.Timer, does the same job. But problem is it repeats. Any way to stop the repetition? –  Jun 01 '15 at 21:09
  • Note: Animation is not important. Show/hide is enough. –  Jun 01 '15 at 21:11