Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am having troubles checking a time that a user inputs against the current time.

The code is in a timer (which is checking the time every second).

What I have got so far:

private void timer1_tick(object sender, EventArgs e)
{
_datetimetocheck = convert.todatetime(txtbxtimeinput.text)

if(_datetimetocheck == _datetimenow)
{
Console.Beep(10,1000);
MessageBox.Show("testing");
}

}

Any suggestions are gratefully received :)

Regards,

Glen Childs
Posted
Comments
Naz_Firdouse 17-Jul-13 4:55am    
what problem you are facing???
Glen Childs 17-Jul-13 4:59am    
The console beep and the message box doesn't run, I'm not sure if the code is at fault or whether it is the way I'm inputting the time into the text box on the form.
Pheonyx 17-Jul-13 4:57am    
The odds of that ever working are slim to non becuase DateTime.Now will have a micoseconds option to it. You need to refine your check to compare the date/time components that actually matter to you.
Glen Childs 17-Jul-13 5:04am    
Oh right ok.

Something like this possibly?
DateTime time = DateTime.Now;
string format = "mm hh"
Pheonyx 17-Jul-13 5:10am    
Yeah, if that is what you are wanting to check against.
How are you ensuring that the user only types in numbers in the correct format in your text box?
There are various ways you could approach. Personally I would look at using a datetime picker as that is what they are designed for:
Sample of doing this
Then you can just compare minute against minute, hour against hour and if need be second against second if you change the formatting in that example.

C#
private void timer1_Tick(object sender, EventArgs e)
{
    DateTime date1 = Convert.ToDateTime(txtbxtimeinput.Text);
    DateTime date2 =DateTime.Now;

    if (date1.ToString() == date2.ToString())
    {
        Console.Beep(37, 1000);
        MessageBox.Show("testing");
    }
}
 
Share this answer
 
Comments
Glen Childs 17-Jul-13 6:33am    
Works fantastic, Cheers
use like this
C#
string format = "M:dd:yyyy h:mm:ss"
           string s= (DateTime.Now.ToString(format));
if(_datetimetocheck == s)
{
//do something
}

if the user enters month,date and year also
else
C#
string format = "h:mm:ss";
string s= (DateTime.Now.ToString(format));

if(_datetimetocheck == s)
{
//do something
}
</pre>
 
Share this answer
 
Comments
Glen Childs 17-Jul-13 6:33am    
Thanks :)
Naz_Firdouse 17-Jul-13 6:42am    
if it solves your solution,mark as answer and vote up
Hi Glen!

I dont know what you try to achieve with the timer running every second, but
if you want something like a validation you should use events like TextChanged or KeyDown on the textbox.

with keydown it could look something like that:

C#
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.Key != Key.Enter) return;

   DateTime dtToCheck = DateTime.Today;

   DateTime inputParsed;
   DateTime.TryParse(textBox1.Text, out inputParsed);

   if (inputParsed != null)
   {
      //uncomment line above if you want to use autocomplete
      //textBox1.Text = inputParsed.ToString();

      if (inputParsed.Equals(dtToCheck))
      {
          //match!
      }
   }
}
 
Share this answer
 
Comments
Glen Childs 17-Jul-13 6:34am    
Cheers
use like this
_datetimetocheck.ToString() == _datetimenow.ToString()
 
Share this answer
 
Comments
Glen Childs 17-Jul-13 6:33am    
Cheers

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