C # show text on shortcut for specific time

Does anyone know how to display text for a specific time on a label or in a text box? Suppose that if I clicked the button, it will display the text entered in the text box on the shortcut for 15 seconds, and then it should disappear.

+3
source share
7 answers

Use Timeravailable inSystem.Timers

at runtime

The Timer class is a Timer control and is used to create a timer at runtime. The following code fragment creates a timer at runtime, sets its property and event handler.

Timer t = new Timer();

t.Interval = 2000;

timer1.Enabled = true;

timer1.Tick += new System.EventHandler(OnTimerEvent);

The code for the event handler is as follows.

private void OnTimerEvent(object sender, EventArgs e)

{

    lbl.Text = DateTime.Now.ToLongTimeString() + "," + DateTime.Now.ToLongDateString();

}

Here is a demo: C # Timer Tutorial

+1

Timer Class.

using System;
//   .
using System.Timers;
public class MyTimer
{
    static int n=0; //.
    public static void Main()
    {
        System.Timers.Timer tmr = new System.Timers.Timer();
        tmr.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
        tmr.Interval=1000; //   1 .
        tmr.Enabled=true; // .
        while(n!=4); //  4 .
    }
    //    Elapsed .
    public static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        //  .
        Console.WriteLine("Hello World!");
        // .
        n++;
    }
}

Referance

+2

-:

javascript, #, .

Windows:

timer .

, , .

+1

. , WinForms WPF, WPF, System.Windows.Timers.Timer.

using System.Windows.Threading;

class MyWindow : Window
{
    public MyWindow()
    {
        _someLabel.Text = "Whatever";
        var timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds( 15 );
        timer.Tick += delegate { _someLabel.Text = String.Empty; };
    }
}
+1

, ... , , .

WPF, , , 5 , . - -, :)

<Window x:Class="WpfApplication12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="150" Width="150">


    <Window.Resources>
        <Style x:Key="Fade" TargetType="TextBox">
            <Style.Triggers>
                <EventTrigger RoutedEvent="TextBox.LostFocus" >
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation    x:Name="z" 
                                                    BeginTime="0:0:0" 
                                                    Duration="0:0:5" 
                                                    From="1.0" 
                                                    To="0" 
                                                    Storyboard.TargetProperty="Opacity" 
                                                    />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <Grid>
        <StackPanel Orientation="Vertical">
            <TextBox x:Name="MyTextBox" Width="100" Height="30" Style="{StaticResource Fade}" />
            <TextBox Width="100" Height="30" Margin="0,5"/>
        </StackPanel>
    </Grid>
</Window>
+1

Timer, , Elapsed . , .

Timer.Interval , .

Enabled true AutoReset false, Timer Elapsed , , .

Timer Elapsed, Interval. , .

Timer.Start Method, Elapsed, Enabled true.

Timer.Stop Method, Elapsed, Enabled false.


.

0

.

Show text and past timer event hide text. Check link

0
source

All Articles