i have wrote the code for a simple traffic light app with wpf elements. I use the timer function. The app works without any exception, but the wanted function isnt there. It only flashes the red or the orange light. I sit for 3 hours and now brain is empty. I try and try without any change. Can you help me please to fix the problem and learn how to use the timer correct? Thanks a lot
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace WPF_Timer
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DispatcherTimer TimerRed, TimerRedOrange, TimerGreen, TimerOrange;
public MainWindow()
{
InitializeComponent();
TimerRed = new DispatcherTimer();
TimerRed.Interval = new TimeSpan(0, 0, 10);
TimerRedOrange = new DispatcherTimer();
TimerRedOrange.Interval = new TimeSpan(0, 0, 5);
TimerGreen = new DispatcherTimer();
TimerGreen.Interval = new TimeSpan(0, 0, 10);
TimerOrange = new DispatcherTimer();
TimerOrange.Interval = new TimeSpan(0, 0, 2);
TimerRed.Tick += TimerRed_Tick;
TimerRedOrange.Tick += TimerRedOrange_Tick;
TimerGreen.Tick += TimerGreen_Tick;
TimerOrange.Tick += TimerOrange_Tick;
}
private void TimerRed_Tick(object sender, EventArgs e)
{
Epsered.Fill = Brushes.Red;
Epseorange.Fill = Brushes.DarkOrange;
Epsegreen.Fill = Brushes.DarkGreen;
TimerRed.Stopp();
TimerRedOrange.Start();
TimerGreen.Stop();
TimerOrange.Stop();
}
private void TimerRedOrange_Tick(object sender, EventArgs e)
{
Epsered.Fill = Brushes.Red;
Epseorange.Fill = Brushes.Orange;
Epsegreen.Fill = Brushes.DarkGreen;
TimerRed.Stop();
TimerGreen.Start();
TimerOrange.Stop();
TimerRedOrange.Stop();
}
private void TimerGreen_Tick(object sender, EventArgs e)
{
Epsegreen.Fill = Brushes.Green;
Epsered.Fill = Brushes.DarkRed;
Epseorange.Fill = Brushes.DarkOrange;
TimerRed.Stop();
TimerRedOrange.Stop();
TimerOrange.Start();
TimerGreen.Stop();
}
private void TimerOrange_Tick(object sender, EventArgs e)
{
Epseorange.Fill = Brushes.Orange;
Epsered.Fill = Brushes.DarkRed;
Epsegreen.Fill = Brushes.DarkGreen;
TimerRed.Start();
TimerRedOrange.Stop();
TimerGreen.Stop();
TimerOrange.Stop();
}
private void BtnStop_Click(object sender, RoutedEventArgs e)
{
Epsered.Fill = Brushes.White;
Epseorange.Fill = Brushes.White;
Epsegreen.Fill = Brushes.White;
TimerRed.Stop();
TimerRedOrange.Stop();
TimerGreen.Stop();
TimerOrange.Stop();
}
private void BtnStart_Click(object sender, RoutedEventArgs e)
{
TimerRed.Start();
TimerRedOrange.Stop();
TimerGreen.Stop();
TimerOrange.Stop();
}
}
}
I think you had a good start; just too many timers. I am trying to learn WPF and I thought this would be a quick project to practice on. Notice in the XAML, I started with the Red Light on. I changed the colors a bit to get more contrast (old eyes.) In the code I cut back to one timer and check the current colors with the if statements. Notice, I did preserve the shorter time for the Orange light.
And here is the C# code...