由网友(∝ノ迩是我的小星星▲)分享简介:我希望用户在文本框。输入数值我得到这个code:私人无效txtType1_Key preSS(对象发件人,重点pressEventArgs E){INT ISNUMBER = 0;!e.Handled = int.TryParse(e.KeyChar.ToString(),出ISNUMBER);}不过,我没有得到...
我希望用户在文本框
。
我得到这个code:
私人无效txtType1_Key preSS(对象发件人,重点pressEventArgs E)
{
INT ISNUMBER = 0;
!e.Handled = int.TryParse(e.KeyChar.ToString(),出ISNUMBER);
}
不过,我没有得到 textbox_Key preSS
活动和 e.KeyChar
在使用WPF。
什么在WPF解决?
编辑:
我犯了一个解决方案!
私人无效txtName_ previewTextInput(对象发件人,TextCompositionEventArgs E)
{
CheckIsNumeric(E);
}
私人无效CheckIsNumeric(TextCompositionEventArgs E)
{
INT结果;
如果(!(int.TryParse(e.Text,出结果)|| e.Text ==。))
{
e.Handled =真实;
}
}
解决方案
保护覆盖无效在previewTextInput(TextCompositionEventArgs E)
{
焦炭C = Convert.ToChar(e.Text);
如果(Char.IsNumber(c))的
e.Handled = FALSE;
其他
e.Handled =真实;
base.On previewTextInput(E);
}
I want user to enter only numeric values in TextBox
.
I got this code:
private void txtType1_KeyPress(object sender, KeyPressEventArgs e)
{
int isNumber = 0;
e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber);
}
But I am not getting textbox_KeyPress
event and e.KeyChar
while using WPF.
Whats the solution in WPF?
Edit:
I made a Solution!
private void txtName_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
CheckIsNumeric(e);
}
private void CheckIsNumeric(TextCompositionEventArgs e)
{
int result;
if(!(int.TryParse(e.Text, out result) || e.Text == "."))
{
e.Handled = true;
}
}
解决方案
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
char c = Convert.ToChar(e.Text);
if (Char.IsNumber(c))
e.Handled = false;
else
e.Handled = true;
base.OnPreviewTextInput(e);
}
相关推荐
最新文章