[英]Why is textbox.Text empty although a value has been entered?
I have this textbox with a KeyPressEventArgs. 我有一个带有KeyPressEventArgs的文本框。
In a dialog I can show the value entered in the textbox via the keychar but not via the textbox.Text member. 在对话框中,我可以显示通过keychar在文本框中输入的值,而不是通过textbox.Text成员。 Once a second character is entered, the textbox.Text member shows only one character, the first one, and so on, so basically, the last charactered isn't shown.
输入第二个字符后,textbox.Text成员仅显示一个字符,第一个字符,依此类推,因此基本上不显示最后一个字符。
Here is the code: 这是代码:
private void textBoxDegrees_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == (char)Keys.Back) //The character represents a backspace
{
e.Handled = false; //Do not reject the input
}
else
{
e.Handled = true; //Reject the input
return;
}
MessageBox.Show(e.KeyChar.ToString());
MessageBox.Show(textBoxDegrees.Text);
}
Any idea what is going on? 知道发生了什么吗?
Regards Crouz 问候克鲁兹
KeyPress
happens before the Text
property is changed, so that you can filter it. KeyPress
发生在Text
属性更改之前,因此您可以对其进行过滤。 Perhaps you want the Changed
event. 也许您想要
Changed
事件。
MessageBox.Show(textBoxDegrees.Text);
This will show your the text in the input before you enter the new value. 在输入新值之前,这将在输入中显示文本。 If it contains
1234
and you press 5
it will show 1234
. 如果它包含
1234
,然后按5
,它将显示1234
。
You can use TextChanged
event handler. 您可以使用
TextChanged
事件处理程序。 It is auto generated when double click on the input. 双击输入将自动生成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.