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

2019-02-19 04:23发布

Does anybody know how to show a text for a particular 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.

7条回答
别忘想泡老子
2楼-- · 2019-02-19 04:25

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.

查看更多
该账号已被封号
3楼-- · 2019-02-19 04:27

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

查看更多
做个烂人
4楼-- · 2019-02-19 04:30

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.

查看更多
别忘想泡老子
5楼-- · 2019-02-19 04:32

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>
查看更多
倾城 Initia
6楼-- · 2019-02-19 04:32

you can use timer class.

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

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-02-19 04:37

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; };
    }
}
查看更多
登录 后发表回答