Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to set a message box against the output text box (which is in days)

that is if I am "0 or > " you have time left etc and if you are " 0 or <" you have no time left"

the code i have used is as follows (but I am unable to use the if statement) against any of the text boxes(i get an error or overload message)

any advice appreciated
C#
private void dateTimePicker4_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                DateTime startDate =
                    (DateTime)dateTimePicker4.Value;

                DateTime endDate =
                    (DateTime)dateTimePicker1.Value;

                DateTime endDate1 =
                    (DateTime)dateTimePicker2.Value;
                DateTime endDate2 =
                    (DateTime)dateTimePicker3.Value;
                TimeSpan ts = endDate.Subtract(startDate);
                TimeSpan ts1 = endDate1.Subtract(startDate);
                TimeSpan ts2 = endDate2.Subtract(startDate);



                textBox1.Text = ts.Days.ToString();
                textBox2.Text = ts1.Days.ToString();
                textBox3.Text = ts2.Days.ToString();
            }
            catch
            {


[edit] Added from Answer 1 :

k sorry (and thanks for the prompt response)

I used the following to try and post a value relating to the textbox1.text
C#
int datet;

datet = int.Parse(textBox1.Text);

if (datet <17)
{

MessageBox.Show("time is short);

}

[/edit]
Posted
Updated 16-Jul-15 9:09am
v3
Comments
[no name] 16-Jul-15 14:57pm    
"I am unable to use the if statement)"... considering that there is no if statement in your code, I am not surprised. And, please do not think that we are going to guess which error you are getting.
[no name] 16-Jul-15 15:26pm    
You should be using TryParse instead of Parse. If the textbox has a string "ABC" in it, your code will throw an exception.
And we still don't know what error you are seeing.

1 solution

Try to use '-' subtraction operator defined for System.DateTime type, instead of System.DateTime.Subtract, it will make your code more readable and help to avoid confusions. As you already know, it produced the return of the type System.TimeSpan. Work with this type to see how many units of time are there.

Also, don't compare integers if you work with time. And, better don't use input from the TextBox to operate with time. If you need, for example, integer value representing days (minutes, hours, etc.) to add to time or subtract from time, better use NumericUpDown control or something similar.

—SA
 
Share this answer
 
v2
Comments
notsocsharp 16-Jul-15 16:23pm    
thanks, I moved the word subtract to the symbol subtract, the error message is no longer however I don't get a message box (yet anyway) i will plug away and close the question

thanks to Wes aday and Sergey for the advice
Sergey Alexandrovich Kryukov 16-Jul-15 16:38pm    
You are very welcome.
If you need more help, you need to be more clear and create a comprehensive code sample, specially to focus on one particular issue.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900