TextBox for number
-
Hi I want to create a text box only for number i create a user control that inherits from textbox and override onkeypress to enter only number but i can't do it . I try with processKeyMessage but i can't again is nobody can help me. Regards' Amirjalaly
-
Hi I want to create a text box only for number i create a user control that inherits from textbox and override onkeypress to enter only number but i can't do it . I try with processKeyMessage but i can't again is nobody can help me. Regards' Amirjalaly
What can't you do? Please be more specific. A simple example using
OnKeyPress
would look like:public class NumericTextBox : TextBox
{
// ...
protected override void OnKeyPress(KeyEventArgs e)
{
if (e.KeyCode < Keys.D0 || e.KeyCode > D9 ||
e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
e.Handled = true;base.OnKeyPress(e);
}
}You can do this by overriding
ProcessDialogChar
orProcessDialogKey
as well.Microsoft MVP, Visual C# My Articles
-
What can't you do? Please be more specific. A simple example using
OnKeyPress
would look like:public class NumericTextBox : TextBox
{
// ...
protected override void OnKeyPress(KeyEventArgs e)
{
if (e.KeyCode < Keys.D0 || e.KeyCode > D9 ||
e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
e.Handled = true;base.OnKeyPress(e);
}
}You can do this by overriding
ProcessDialogChar
orProcessDialogKey
as well.Microsoft MVP, Visual C# My Articles
The problem with this approach is that it doesn't stop anyone from pasting a string, and this will block Backspace key as well. It will also not accept decimal numbers. I needed a numeric text field control a while ago, and I thought it would be as simple as overriding OnKeyPress, but there is more to it.
-
The problem with this approach is that it doesn't stop anyone from pasting a string, and this will block Backspace key as well. It will also not accept decimal numbers. I needed a numeric text field control a while ago, and I thought it would be as simple as overriding OnKeyPress, but there is more to it.
You can change the sample to accept backspace, delete, and the decimal point. In order to validate pasted text, override
OnTextChanged
orOnValidating
. It's possible to do what you need, you just have to override the right methods as necessary.Microsoft MVP, Visual C# My Articles
-
The problem with this approach is that it doesn't stop anyone from pasting a string, and this will block Backspace key as well. It will also not accept decimal numbers. I needed a numeric text field control a while ago, and I thought it would be as simple as overriding OnKeyPress, but there is more to it.
Try this one: private void textBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { this.validValue = this.Text; } private void textBox_TextChanged(object sender, System.EventArgs e) { try { int.Parse(this.Text); } catch (System.FormatException) { this.Text = this.validValue; } }
-
Try this one: private void textBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { this.validValue = this.Text; } private void textBox_TextChanged(object sender, System.EventArgs e) { try { int.Parse(this.Text); } catch (System.FormatException) { this.Text = this.validValue; } }
Almost... private void textBox_TextChanged(object sender, System.EventArgs e) { try { if(this.Text.Length > 0) int.Parse(this.Text); this.validValue = this.Text } catch (System.FormatException) { this.Text = this.validValue; } }
-
Almost... private void textBox_TextChanged(object sender, System.EventArgs e) { try { if(this.Text.Length > 0) int.Parse(this.Text); this.validValue = this.Text } catch (System.FormatException) { this.Text = this.validValue; } }
Interesting, I will consider this in my application. Never needed to look for an empty string, because all my TextBoxes, i use this for, have default values. But better be prepared for all cases :-) By the way, what happens, if the Parse method is called with a string with Length = 0?
-
Interesting, I will consider this in my application. Never needed to look for an empty string, because all my TextBoxes, i use this for, have default values. But better be prepared for all cases :-) By the way, what happens, if the Parse method is called with a string with Length = 0?
int.Parse throws a FormatException when given a zero length string so that check is definitely necessary (at least it is for my application)