|
-
March 20th, 2011, 01:13 PM
#1
[RESOLVED] Best way to validate time from user?
Hello, everyone. I have two text boxes (timeTakenA and timeTakenB), and I want the user to be able to enter a time into these text boxes (hh:mm:ss). Then when a button is pressed, the two times are added together and a total time taken is displayed.
I must admit, I'm really struggling to find a good way of doing this. Can anyone give me some ideas?
Thanks in advance.
**I'm using Visual Studio 2010 .NET 4
-
March 20th, 2011, 02:40 PM
#2
Re: Best way to validate time from user?
how about the Timespan struct ... have you considered that ? might work for ya.
Code:
string[] time1, time2;
string result;
time1 = textbox1DotText.Split(':');
time2 = textbox2DotText.Split(':');
result = (new TimeSpan(Int32.Parse(time1[0]), Int32.Parse(time1[1]), Int32.Parse(time1[2])) +
new TimeSpan(Int32.Parse(time2[0]), Int32.Parse(time2[1]), Int32.Parse(time2[2]))).ToString();
// Trust in The Force, Luke ....
Last edited by ThermoSight; March 20th, 2011 at 04:16 PM.
-
March 20th, 2011, 03:04 PM
#3
Re: Best way to validate time from user?
I did this quickly, it could probably be a little cleaner, but should work:
Code:
namespace add_date_time
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnAddTimes_Click(object sender, EventArgs e)
{
int AHours = int.Parse(txtBoxTimeA.Text.Split(':')[0]);
int AMinutes = int.Parse(txtBoxTimeA.Text.Split(':')[1]);
int ASeconds = int.Parse(txtBoxTimeA.Text.Split(':')[2]);
int BHours = int.Parse(txtBoxTimeB.Text.Split(':')[0]);
int BMinutes = int.Parse(txtBoxTimeB.Text.Split(':')[1]);
int BSeconds = int.Parse(txtBoxTimeB.Text.Split(':')[2]);
int TotalHours = AHours+BHours;
int TotalMinutes = AMinutes+BMinutes;
int TotalSeconds = ASeconds+BSeconds;
while (TotalSeconds > 60 | TotalSeconds == 60)
{
TotalSeconds = (TotalSeconds - 60);
TotalMinutes = (TotalMinutes + 1);
}
while (TotalMinutes > 60 | TotalMinutes == 60)
{
TotalMinutes = (TotalMinutes - 60);
TotalHours = (TotalHours + 1);
}
int TotalDays = 0;
while (TotalHours > 24 | TotalHours == 24)
{
TotalHours = (TotalHours - 24);
TotalDays = (TotalDays + 1);
}
txtBoxTotalTimeTaken.Text = TotalDays + "d :" + TotalHours + "h :" + TotalMinutes + "m :" + TotalSeconds + "s";
}
}
}
Last edited by saldous; March 20th, 2011 at 05:03 PM.
-
March 20th, 2011, 03:33 PM
#4
Re: Best way to validate time from user?
I can't thank you guys enough. It's much appreciated. Your example worked perfectly Saldous. Good Job.
-
March 20th, 2011, 05:02 PM
#5
Re: [RESOLVED] Best way to validate time from user?
ThermoSight's code is a lot nicer. I learnt something too.
-
March 21st, 2011, 03:39 AM
#6
Re: [RESOLVED] Best way to validate time from user?
Ahh...I didn't see that edit. Thanks ThermoSight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|