输入时间并根据System.DateTime进行检查 [英] Inputting a time and checking it against System.DateTime
问题描述
您好,
我在检查用户输入当前时间的时间时遇到了麻烦。
代码在一个计时器(每秒检查一次)。
到目前为止我得到了什么:
private void timer1_tick(object sender,EventArgs e)
{
_datetimetocheck = convert.todatetime(txtbxtimeinput.text)
if(_datetimetocheck == _datetimenow)< br $>
{
Console.Beep(10,1000);
MessageBox.Show(testing);
}
}
感谢任何建议:)
问候,
Glen Childs
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
推荐答案
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");
}
}
像这样使用
use like this
string format = "M:dd:yyyy h:mm:ss"
string s= (DateTime.Now.ToString(format));
if(_datetimetocheck == s)
{
//do something
}
如果用户输入月份,日期和年份也
else
if the user enters month,date and year also
else
string format = "h:mm:ss";
string s= (DateTime.Now.ToString(format));
if(_datetimetocheck == s)
{
//do something
}
</pre>
嗨Glen!
我不知道你试图通过每秒运行的计时器实现什么,但是如果你想要像验证这样的东西你应该使用像文本框上的TextChanged
或KeyDown
。
with keydown它看起来像这样:
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 likeTextChanged
orKeyDown
on the textbox.
with keydown it could look something like that:
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!
}
}
}
这篇关于输入时间并根据System.DateTime进行检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!