开发者

c# show a text in a label for a particular time

Does anybody know how to show a text for a particu开发者_JAVA百科lar time in a label or in a textbox? Suppose if I clicked a button it show the text typed in the textbox in a label for 15 seconds and then it should disappear.


Timer Class.

Code Example

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

  • C# Timer Tutorial
  • C# & Timer


You can use a timer. You don't say if this is WinForms or WPF, so I'll assume WPF, but you can use a System.Windows.Timers.Timer just as well.

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; };
    }
}


Make use of Timer, available in System.Timers

Run-time

Timer class represents a Timer control and used to create a Timer at run-time. The following code snippet creates a Timer at run-time, sets its property and event handler.

Timer t = new Timer();

t.Interval = 2000;

timer1.Enabled = true;

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

The event handler code looks like following.

private void OnTimerEvent(object sender, EventArgs e)

{

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

}

Here is demo : C# Timer Tutorial


Assuming a web page:

You can do this through javascript, you don't want to do this in C# as that's handled server side.

Assuming a windows app:

You could use a timer to remove the label after a few seconds.

Try specifying what type of application you're working on in the question as it makes it easier to give a concise answer.


I'm going to take a wild guess like everyone else... if this solution is not useful at this time then it maybe useful for others searching on this subject.

If you are using WPF it is trivial, check this complete sample which fades the textbox out over 5 seconds once it has lost focus. The second textbox is there simply to give you something to move focus to :)

<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>


you can use timer class.

Show the text and the elapsed event of timer hide the text. Check the link


Use the Timer component which allows you to specify a recurring interval at which the Elapsed event is raised in your application. You can then handle this event to provide regular processing.

The Timer.Interval Property is used to set the interval at which to raise the Elapsed event.

If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses.

The Timer component raises the Elapsed event, based on the value of the Interval property. You can handle this event to perform the processing you need.

Use the Timer.Start Method to start raising the Elapsed event by setting Enabled to true.

Use the Timer.Stop Method to stop raising the Elapsed event by setting Enabled to false.


Refer to the following sample.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜