Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Interesting Times...

Interesting Times...

Scheduled Pinned Locked Moved C#
questionhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Illegal Operation
    wrote on last edited by
    #1

    Hi! I have an interesting problem. I have an application that contains two textboxes. The user enters his StartTime in one textbox and his EndTime in the other. I have a calculation method that calculated the total Hours between these two variables. The problem I am having is that when the user inserts the starttime say 8:00 and finish time say 7:00 you will get a -1 hour. How can I prevent users from entering negative values?? My method looks like this:

        private void CalculateHoursWorked()
        {
            string startTime = Convert.ToDateTime(txtStartTime.Text).ToShortTimeString();
            DateTime startDate = Convert.ToDateTime(startTime);
            string endTime = Convert.ToDateTime(txtEndTime.Text).ToShortTimeString();
            DateTime endDate = Convert.ToDateTime(endTime);
    
            TimeSpan ts = endDate.Subtract(startDate);
    
            HoursWorked = ts.Hours.ToString();
            MinutesWorked = ts.Minutes.ToString();
        }
    

    Illegal Operation

    D N 2 Replies Last reply
    0
    • I Illegal Operation

      Hi! I have an interesting problem. I have an application that contains two textboxes. The user enters his StartTime in one textbox and his EndTime in the other. I have a calculation method that calculated the total Hours between these two variables. The problem I am having is that when the user inserts the starttime say 8:00 and finish time say 7:00 you will get a -1 hour. How can I prevent users from entering negative values?? My method looks like this:

          private void CalculateHoursWorked()
          {
              string startTime = Convert.ToDateTime(txtStartTime.Text).ToShortTimeString();
              DateTime startDate = Convert.ToDateTime(startTime);
              string endTime = Convert.ToDateTime(txtEndTime.Text).ToShortTimeString();
              DateTime endDate = Convert.ToDateTime(endTime);
      
              TimeSpan ts = endDate.Subtract(startDate);
      
              HoursWorked = ts.Hours.ToString();
              MinutesWorked = ts.Minutes.ToString();
          }
      

      Illegal Operation

      D Offline
      D Offline
      Dan Mos
      wrote on last edited by
      #2

      Illegal Operation wrote:

      How can I prevent users from entering negative values??

      First: you should use dateTimePicker(s) instead of text boxes (a masked textBox could do the job but => more work) As for negative result create a function to swap dates. Something like:

      private void SwapDates(){
      if(start.CompareTo(end)<0)//start and end are two datetimes
      {
      DateTime tmp = start;
      start=end;
      end=tmp;
      }
      }

      1 Reply Last reply
      0
      • I Illegal Operation

        Hi! I have an interesting problem. I have an application that contains two textboxes. The user enters his StartTime in one textbox and his EndTime in the other. I have a calculation method that calculated the total Hours between these two variables. The problem I am having is that when the user inserts the starttime say 8:00 and finish time say 7:00 you will get a -1 hour. How can I prevent users from entering negative values?? My method looks like this:

            private void CalculateHoursWorked()
            {
                string startTime = Convert.ToDateTime(txtStartTime.Text).ToShortTimeString();
                DateTime startDate = Convert.ToDateTime(startTime);
                string endTime = Convert.ToDateTime(txtEndTime.Text).ToShortTimeString();
                DateTime endDate = Convert.ToDateTime(endTime);
        
                TimeSpan ts = endDate.Subtract(startDate);
        
                HoursWorked = ts.Hours.ToString();
                MinutesWorked = ts.Minutes.ToString();
            }
        

        Illegal Operation

        N Offline
        N Offline
        Nick Coombs
        wrote on last edited by
        #3

        You should check if the time is negative. If so show an error that clearly explains that negative time is not allowed and why the time the user entererd was negative. Then highlight one of the times in red

        FolderTrack

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups