Home > Net >  Adding a countdown timer to textblock WPF
Adding a countdown timer to textblock WPF

Time:11-01

I want to implement a countdown timer that use to work on CMD into a textblock with wpf.

I can't figure it out how to implement that code into a textblock.

  DateTime daysLeft1 = DateTime.Parse("1/02/2022 12:00:01 AM");
                DateTime startDate1 = DateTime.Now;


                TimeSpan t = daysLeft1 - startDate1;
                string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds until launch.",
                    t.Days, t.Hours, t.Minutes, t.Seconds);

Thank you for your help ! .

EDIT: The code Below seem to work but I have error CS0120

"An object reference is required for the non-static field, method, or property 'TextBlock.Text' "

public partial class justdancetrailer : Window
{
    private readonly DispatcherTimer Timer = new DispatcherTimer();
    private readonly DateTime daysLeft;

    public justdancetrailer()
    {
        InitializeComponent();

        daysLeft = DateTime.Parse("1/02/2022 12:00:00 AM");

        Timer.Interval = TimeSpan.FromSeconds(1.0); ;
        Timer.Tick  = Timer_Tick;
        Timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        TimeSpan t = daysLeft.Subtract(DateTime.Now);
        if (t.Seconds >= 0)
        {
            string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds until launch.",
                t.Days, t.Hours, t.Minutes, t.Seconds);

            



            TextBlock.Text = countDown;

            daysLeft.AddSeconds(-1);
        }
        else
        {
            Timer.Stop();
        }
    }

    private void trailer_play(object sender, RoutedEventArgs e)
    {
        trailer.Play();
    }

    private void trailer_stop(object sender, RoutedEventArgs e)
    {
        trailer.Stop();
        trailer.Close();
    }

    private void trailer_pause(object sender, RoutedEventArgs e)
    {
        trailer.Pause();

    }
    private void ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        trailer.Volume = e.NewValue;
    }


}

}

And this is the TextBlock :

<TextBlock x:Name="countDown" HorizontalAlignment="Left" Margin="40,100,0,0" TextWrapping="Wrap"  VerticalAlignment="Top"
               Grid.ColumnSpan="2" Width="250" Height="58" Foreground="White"
               Grid.Column="3" Grid.Row="1"
               Text=""/>

CodePudding user response:

Supposing you need an interval equals 1 second.

    private readonly DispatcherTimer Timer = new DispatcherTimer();
    private readonly DateTime daysLeft;

    public MainWindow()
    {
        InitializeComponent();

        daysLeft = DateTime.Parse("1/02/2022 12:00:00 AM");
        
        Timer.Interval = TimeSpan.FromSeconds(1.0); ;
        Timer.Tick  = Timer_Tick;
        Timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        TimeSpan t = daysLeft.Subtract(DateTime.Now);
        if (t.Seconds >= 0)
        {
            string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds until launch.",
                t.Days, t.Hours, t.Minutes, t.Seconds);

            textBlock.Text = countDown;
            daysLeft.AddSeconds(-1);
        }
        else
        {
            Timer.Stop();
        }
    }
  • Related