Create a Timer from userInput with c#. Display like h:m:s

  • Thread starter William Lindahl
  • Start date
W

William Lindahl

Guest
(please bare with me, i'm new to the developing world) I hope that someone can point me in the right direction.

I want to create a countdown timer. Where the user enter the timespan and then it displays the countdown from userInput like h:m:s.

I've tried using tick, stopwatch etc. but can't seem to figure out which is the right way to go.

Also want to point out that I don't expect the solution just which methods and tools i should use to create this. The mainproblem for me is how to read the time from the user.

Here is one of my tries (this only starts from 0 end continues until stopped) This is in WPF but my goal is to when i figure out how i should do this I will move it to Xamarin so I can use the application in my phone :

namespace StudyTimer2._0
{

public partial class MainWindow : Window
{
readonly DispatcherTimer dispatcherTimer = new DispatcherTimer();
readonly Stopwatch stopWatch = new Stopwatch();
string currentTime = string.Empty;

public MainWindow()
{
InitializeComponent();
dispatcherTimer.Tick += new EventHandler(Dt_tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);

}

private void Dt_tick(object sender, EventArgs e)
{
if (stopWatch.IsRunning)
{
TimeSpan timeSpan = stopWatch.Elapsed;
currentTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10);
RunningTime.Text = currentTime;
}
}

private void StartButton_Click(object sender, RoutedEventArgs e)
{
stopWatch.Start();
dispatcherTimer.Start();


}

private void StopButton_Click(object sender, RoutedEventArgs e)
{
if (stopWatch.IsRunning)
stopWatch.Stop();
}

private void AddButton_Click(object sender, RoutedEventArgs e)
{
TimeElapsedItems.Items.Add(currentTime);
}
}
}

Continue reading...
 
Top