C# How to make the time go backwards?

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2022 Views - Last Post: 03 October 2017 - 05:13 AM Rate Topic: -----

#1 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

C# How to make the time go backwards?

Posted 01 October 2017 - 07:39 PM

I am trying to make it so when I press a button the timer starts, but then when I press another button the timer starts to go backwards what class can I use to achieve this? I tried Stopwatch and timespan but don't think it has what I need.
Is This A Good Question/Topic? 0
  • +

Replies To: C# How to make the time go backwards?

#2 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: C# How to make the time go backwards?

Posted 01 October 2017 - 09:00 PM

I am gathering that you want count down? I think the issue is that you need to make a control display the time value you need.

Ie, the below code will populate a TextBlock with time counting down from 60 secs.

private void Button1_onclick(object sender, RoutedEventArgs e)
        {
            DispatcherTimer countTimer = null;
            TimeSpan countDown;
            countDown = TimeSpan.FromSeconds(60);

            countTimer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
            {
                textblock1.Text = countDown.ToString();
                if (countDown == TimeSpan.Zero) countTimer.Stop();
                countDown = countDown.Add(TimeSpan.FromSeconds(-1));
            }, Application.Current.Dispatcher);

            countTimer.Start();
        }


and the XAML,
<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="35"/>
            <RowDefinition Height="35"/>
            </Grid.RowDefinitions>
        <Button Grid.Row="0" Name="button1" Click="Button1_onclick"></Button>
        <TextBlock Grid.Row="1" Name="textblock1" ></TextBlock>
    </Grid>


Then you can manipulate the timer how ever you want to display the time and intervals you need.
Was This Post Helpful? 0
  • +
  • -

#3 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: C# How to make the time go backwards?

Posted 01 October 2017 - 09:02 PM

Yeah sort of a countdown, but I only want it to countdown from the seconds,hours,minutes that the timer is currently at. Thanks for the code also.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,121
  • Joined: 05-May 12

Re: C# How to make the time go backwards?

Posted 01 October 2017 - 09:04 PM

Just need the the built in DateTime or TimeSpan and any one of the Timer classes. Since you said you are using those already, I suspect that you are not using them correctly. Why not post your code so that we can see what you are doing?
Was This Post Helpful? 0
  • +
  • -

#5 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: C# How to make the time go backwards?

Posted 01 October 2017 - 09:18 PM

I didn't have any good decent code then, but now I sorta do here it is:

  public partial class TasksPage : ContentPage
    {
        //used to start the time
        private Stopwatch _stopWatch;

        //used to reduce time
        private TimeSpan _timeSpan;

        public TasksPage()
        {
            InitializeComponent();
            //initialize object so we dont get null exception error
            _stopWatch = new Stopwatch();
        }

        void TimeStarted(object sender, EventArgs e)
        {
            //get the current button and store in variable so we can check the text of button to do certain actions for each button
            var currentButton = sender as Button;

            //check if  text is equal to this text
            if (currentButton.Text.Equals("button1"))
            {
                //start the timer
                _stopWatch.Start();



                //format the timelabel text so it displays in a hours,minutes, seconds fashion
                timeLabel.Text = _stopWatch.Elapsed.ToString(@"hh\:mm\:ss");
            }

            if (currentButton.Text.Equals("button2"))
            {


                timeLabel.Text = TimeSpan.FromSeconds(_stopWatch.Elapsed.TotalSeconds).ToString(@"hh\:mm\:ss");
                
            }

            if (currentButton.Text.Equals("button3"))
            {

                timeLabel.TextColor = Color.White;
            }

        }
    }


Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,373
  • Joined: 02-June 10

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 01:08 AM

That code isn't for your Xamarin project, isn't it? Oh... Wait ContentPage... It is... Ummm... That's just for testing and whacking around with, right? That's not going in a final project like that, is it?
Was This Post Helpful? 0
  • +
  • -

#7 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 01:20 AM

Yes it is, what is the problem?
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,373
  • Joined: 02-June 10

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 01:42 AM

Primarily the 2003 way of doing everything. This looks like some old WinForms project. It hints at someone creating the UI first, then figuring out how to make it work second. Planning the logic of the app... well... Someplace far further down the line. Which is all basically backwards. When building software the planning comes first. The logic second. The UI last.

  • Directly manipulating the UI like that (changing the text of the label and the button)...
  • Logic that is tightly coupled to the names of the buttons on the UI...
    • If (button name is xxx) do this
    • if (button name is yyy) do that

  • The output of the time is being formatted according to a hardcoded string, instead of a setting that could be changed in the app's preferences, or using the format set at the OS level. What if the user prefers a 24 clock? What if the time runs beyond hours and into days?
  • You mentioned "button click" in your text... I assume that means you're actually responding to the .clicked event on the button instead of using a command.
  • The names of the controls... "Button1, Button2, Button3"... Really? You had to know that felt cheesy while you were typing it, but you just did it anyway.


All of this adds up to coding in a way that would be expected of a first year student on their homework for 'Intro to C#'. But its so far off from an acceptable way to create a modern MVVM application (WPF or Xamarin, it doesn't matter which).

Here's a hint I've given at least 100 times here: If you are naming your controls in XAML - you're probably already going down the wrong path.

I really, really, really urge you to sign up for Xamarin University. Start with the C# courses then work into the Xamarin Forms and mobile development courses. That path will help update your planning and get you away from this 15 year old approach that really just doesn't support a modern application these days.

This post has been edited by tlhIn`toq: 02 October 2017 - 01:47 AM

Was This Post Helpful? 0
  • +
  • -

#9 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 02:12 AM

I tried Xamarin University and didn't get anywhere. There weren't clear instructions from what I remember, I will check again. There is no 24 hour clock, its going to be only in 1 format. The names of the button controls that will be changed later. With the C# I don't think I have any more patience with going through normal c# courses, I have done fundamentals at least 5 times, different books, heaps of tutorials, and recently did 2 c# courses to fill some holes in my learning it was intermediate and advanced stuff.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,121
  • Joined: 05-May 12

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 05:26 AM

View PostDialupp, on 02 October 2017 - 05:12 AM, said:

The names of the button controls that will be changed later.

But with correctly written XAML using the MVVM pattern, you wouldn't even have to change the names of the buttons because the buttons will not have any names because you'll be binding to a view model. Using the adapter pattern, you wouldn't even have to change the bindings in the XAML -- just update at most 3 lines in the view model.
Was This Post Helpful? 1
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,373
  • Joined: 02-June 10

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 05:27 AM

View PostDialupp, on 02 October 2017 - 04:12 AM, said:

and recently did 2 c# courses to fill some holes in my learning it was intermediate and advanced stuff.


I'm sorry to break it to you... but you don't have basics down - certainly not for 21st century design and cross-platform development. Most of that wouldn't have been acceptable in WPF in 2012. Moving on to intermediate and advanced... well... You don't start building the 2nd story on top of a shaking first story, on top of an unstable foundation.

Just because you're frustrated with, and don't grasp the courses you went through doesn't mean you can just skip them or they weren't important. You need those foundation principles. They are the basis of your app and what you build on top of.

If the construction metaphors aren't clicking how about navigation?
On a 10,000 mile trip you can be off by 1 degree in the last mile and still see your destination.
But if you start off 1 degree off course and continue for the duration of all 10,000 miles... that slight error will have you missing your destination by an entire continent.

I'm not saying this to be mean or tear you down. I'm saying this because I want to see you succeed. I don't want to see you invest 5,000 hours along this direction and have to post

Quote

"Well I did xyz and now I've painted myself into a corner. How can I fix all this without having to throw out my last 6 months work?"
Its a lot better to spend a couple hundred hours on education than to waste a couple thousand on work that has to be tossed.
Was This Post Helpful? 1
  • +
  • -

#12 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,121
  • Joined: 05-May 12

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 05:40 AM

View PostDialupp, on 02 October 2017 - 05:12 AM, said:

There is no 24 hour clock, its going to be only in 1 format.

Depending on which country you are in, or on the user's settings for their local, they may want to see the time as one of the following formats:
hhmm
hh:mm
hh.mm
hh hr mm
hmm
h:mm
h.mm
h hr m

where:
hh - number of hours with leading zero if needed
mm - number of minutes with leading zero if needed
h - number of hours without leading zero
m - number of minutes without leading zero
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,373
  • Joined: 02-June 10

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 06:01 AM

View PostDialupp, on 02 October 2017 - 05:12 AM, said:

There is no 24 hour clock, its going to be only in 1 format.

That attitude of "I'm going to dictate the operation of the app" really is not tolerated by customers any more. - Nothing against you personally. Its just the prevailing attitude and expectation of the market. User's don't put up with that from developers because they see they don't have to. When 100,000 other apps out there are user friendly they don't accept user hostile.

Plus there is the entire issue of having the market place/store even accept your app. Because the OS makers require software to honor user settings from the OS, its is becoming increasingly more expected. At some point in the near future NOT honoring settings and accessibility systems for special needs will be grounds for not accepting an app in the store; just like failing to use IPv6 or there being any unhandledexceptions or attempts to restart the app from within the app, and the myriad of other reasons that will keep your app from being accepted.

This post has been edited by tlhIn`toq: 02 October 2017 - 12:57 PM
Reason for edit:: Fixed quote attribution.

Was This Post Helpful? 1
  • +
  • -

#14 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,121
  • Joined: 05-May 12

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 06:46 AM

+100.

Anyway, back on topic regarding the problem. This is a really simple issue of adding or subtracting time from a TimeSpan.

Rough pseudo code:
class CountdownStopwatch
{
     TimeSpan TimeLeft = { get; private set; }
     DateTime StartTime { get; private set; }

     public void CountdownStopwatch(TimeSpan timeLeft)
     {
         TimeLeft = timeLeft;
     }

     public void Start()
     {
         StartTime = DateTime.Now;
     }

     public void Stop()
     {
         TimeLeft -= DateTime.Now - StartTime;
     }
}


Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,121
  • Joined: 05-May 12

Re: C# How to make the time go backwards?

Posted 02 October 2017 - 07:28 AM

And if the TimeSpan coupled with a Timer doesn't work, you could always try a flux capacitor and 1.21 gigawatts of power... Good luck with the DeLorean. :)
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2