WPF TextBox 验证输入
转载
02 |
private void txt_time_KeyDown( object sender, KeyEventArgs e)
|
04 |
if (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)))
|
11 |
private void txt_time_TextChanged( object sender, TextChangedEventArgs e)
|
13 |
var textBox = sender as TextBox;
|
14 |
TextChange[] change = new TextChange[e.Changes.Count];
|
15 |
e.Changes.CopyTo(change, 0);
|
17 |
int offset = change[0].Offset;
|
18 |
if (change[0].AddedLength > 0)
|
21 |
if (!Double.TryParse(textBox.Text, out num))
|
23 |
textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
|
24 |
textBox.Select(offset, 0);
|
02 |
private void txtAge_KeyDown( object sender, KeyEventArgs e)
|
04 |
TextBox txt = sender as TextBox;
|
06 |
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal)
|
08 |
if (txt.Text.Contains( "." ) && e.Key == Key.Decimal)
|
15 |
else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
|
17 |
if (txt.Text.Contains( "." ) && e.Key == Key.OemPeriod)
|
31 |
private void txtAge_TextChanged( object sender, TextChangedEventArgs e)
|
33 |
TextBox textBox = sender as TextBox;
|
34 |
TextChange[] change = new TextChange[e.Changes.Count];
|
35 |
e.Changes.CopyTo(change, 0);
|
37 |
int offset = change[0].Offset;
|
38 |
if (change[0].AddedLength > 0)
|
41 |
if (!Double.TryParse(textBox.Text, out num))
|
43 |
textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
|
44 |
textBox.Select(offset, 0);
|
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。