使用C#,visualstudio 2010,silverlight 2个文本框,1个名称为
的名称textbox1,textbox2,button1
textbox1 - input allow only hours (HH)
textbox2 - input allow only minutes (mm)
button1_click事件
我想验证带有以下条件的textbox1,textbox2
textbox1, allow only hours between 00 to 23
textbox2, allow only minutes between 00 to 59
如何编写代码或函数。需要代码帮助
答案 0 :(得分:0)
我认为这是你正在努力实现的目标
public partial class MainPage:UserControl { 公共MainPage() { 的InitializeComponent(); txHour.MaxLength = 2; txtMinutes.MaxLength = 2; txHour.KeyDown + = new KeyEventHandler(txt_KeyDown); txtMinutes.KeyDown + = new KeyEventHandler(txt_KeyDown);
}
private void txt_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key < Key.D0 || e.Key > Key.D9)
{
if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
{
if (e.Key != Key.Back && e.Key != Key.Shift && e.Key != Key.Tab)
{
e.Handled = true;
}
}
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
bool isvalid = false;
int x, y;
if (int.TryParse(txHour.Text, out x) && int.TryParse(txtMinutes.Text, out y))
{
if (x >= 0 && x <= 23 && y >= 0 && y <= 59)
{
isvalid = true;
}
}
if (!isvalid)
{
MessageBox.Show("InValidTime");
}
}
}