Interesting Times...
-
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
-
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
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;
}
} -
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
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