Reputation:
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.
Upvotes: 1
Views: 10726
Reputation: 9653
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"
Upvotes: 6
Reputation: 1553
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;
}
Upvotes: 6
Reputation: 155
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
Upvotes: 0