By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 935 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

Replacing keyboard input in textbox

P: n/a
Hi,

How can I restrict the keyboard input in a textBox to 0..9? In the
keydown event KeyValue is get only. Where can I alter the keyboard input?

Mvg,
Nov 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Jo,

Use the keyup event and than by instance the e.value
(Don't forget on by instance value 8 because that is the backspace).

I hope this helps,

Cor
Nov 17 '05 #2

P: n/a
Cor Ligthert schreef:
Jo,

Use the keyup event and than by instance the e.value
(Don't forget on by instance value 8 because that is the backspace).

I hope this helps,

Cor

Hi Cor,

This is the code I use:

private void textBoxOrderid_KeyUp(object sender,
system.Windows.Forms.KeyEventArgs e)
{
if ((char)e.KeyValue < '0' || (char)e.KeyValue > '9' || e.KeyValue == 8)
{
e.Handled = true;
}
}

I only want the keys 0 to 9 to appear in the textbox. However the method
above does not work.

Any other suggestions?
Jo.
Nov 17 '05 #3

P: n/a
Override TextBox class like following :

class TextBoxNum:TextBox {
private String _Text="";
private Regex regex=new Regex("^[0-9]*$",RegexOptions.Compiled);
protected override void OnTextChanged(EventArgs e) {
if (Text==_Text) return ;//Don't recurs
if (regex.Match(Text).Success) _Text=Text;
else {
int pos=SelectionStart+_Text.Length-Text.Length;
Text=_Text;
SelectionStart=pos<0?0:pos;
}
}
}
}

And declare in your code textBoxOrderid as a TextBoxNum :
TextBoxNum textBoxOrderid
Hope it helps,

Ludovic SOEUR.
"Jo Segers" <jo*******@alro.be> a écrit dans le message de
news:Ot**************@tk2msftngp13.phx.gbl...
Hi,

How can I restrict the keyboard input in a textBox to 0..9? In the
keydown event KeyValue is get only. Where can I alter the keyboard input?

Mvg,

Nov 17 '05 #4

P: n/a
Jo,

Is it not something more as this that you want.
{
if ((char)e.KeyValue < '0' || (char)e.KeyValue > '9' || e.KeyValue == 8)
{
textBox1.SelectionStart = (int)textBox1.Text.Length - 1;
textBox1.SelectionLength = 1;
e.Handled = true;
}
}


Be aware that you should use the validating event as well to check that
nobody paste something wrong in, or set the property that allows that to
false.

I hope this helps,

Cor
Nov 17 '05 #5

P: n/a
Cor Ligthert schreef:
Jo,

Is it not something more as this that you want.

{
if ((char)e.KeyValue < '0' || (char)e.KeyValue > '9' || e.KeyValue == 8)
{

textBox1.SelectionStart = (int)textBox1.Text.Length - 1;
textBox1.SelectionLength = 1;

e.Handled = true;
}
}

Be aware that you should use the validating event as well to check that
nobody paste something wrong in, or set the property that allows that to
false.

I hope this helps,

Cor

Thanks a lot for the usefull information.

Yours sincerely,

Jo Segers.
Nov 17 '05 #6

P: n/a
Override TextBox class like following :

class TextBoxNum:TextBox {
private String _Text="";
private Regex regex=new Regex("^[0-9]*$",RegexOptions.Compiled);
protected override void OnTextChanged(EventArgs e) {
if (Text==_Text) return ;//Don't recurs
if (regex.Match(Text).Success) _Text=Text;
else {
int pos=SelectionStart+_Text.Length-Text.Length;
Text=_Text;
SelectionStart=pos<0?0:pos;
}
}
}
}

And declare in your code textBoxOrderid as a TextBoxNum :
TextBoxNum textBoxOrderid
Hope this helps,

Ludovic SOEUR.

"Jo Segers" <jo*******@alro.be> a écrit dans le message de
news:ei**************@TK2MSFTNGP09.phx.gbl...
Cor Ligthert schreef:
Jo,

Is it not something more as this that you want.

{
if ((char)e.KeyValue < '0' || (char)e.KeyValue > '9' || e.KeyValue == 8)
{

textBox1.SelectionStart = (int)textBox1.Text.Length - 1;
textBox1.SelectionLength = 1;

e.Handled = true;
}
}

Be aware that you should use the validating event as well to check that
nobody paste something wrong in, or set the property that allows that to
false.

I hope this helps,

Cor

Thanks a lot for the usefull information.

Yours sincerely,

Jo Segers.

Nov 17 '05 #7

P: n/a
Ludovic SOEUR schreef:
Override TextBox class like following :

class TextBoxNum:TextBox {
private String _Text="";
private Regex regex=new Regex("^[0-9]*$",RegexOptions.Compiled);
protected override void OnTextChanged(EventArgs e) {
if (Text==_Text) return ;//Don't recurs
if (regex.Match(Text).Success) _Text=Text;
else {
int pos=SelectionStart+_Text.Length-Text.Length;
Text=_Text;
SelectionStart=pos<0?0:pos;
}
}
}
}

And declare in your code textBoxOrderid as a TextBoxNum :
TextBoxNum textBoxOrderid
Hope this helps,

Ludovic SOEUR.


Thanks for all the information. I got it working the following way:

private void textBoxOrderid_TextChanged(object sender, System.EventArgs e)
{
string temp = textBoxOrderid.Text;
string newval = "";
for (int i = 0; i < temp.Length; i++)
{
if (temp[i] >= '0' && temp[i] <= '9')
{
newval += temp[i];
}
}
if (temp != newval)
{
textBoxOrderid.Text = newval;
textBoxOrderid.SelectionStart = textBoxOrderid.Text.Length;
}
}

Any remarks?

Jo.
Nov 17 '05 #8

P: n/a
OK, that's nearly the same but you should keep Regex instead of your loop if
you need efficiency.

"Jo Segers" <jo*******@alro.be> a écrit dans le message de
news:e9**************@TK2MSFTNGP14.phx.gbl...
Ludovic SOEUR schreef:
Override TextBox class like following :

class TextBoxNum:TextBox {
private String _Text="";
private Regex regex=new Regex("^[0-9]*$",RegexOptions.Compiled);
protected override void OnTextChanged(EventArgs e) {
if (Text==_Text) return ;//Don't recurs
if (regex.Match(Text).Success) _Text=Text;
else {
int pos=SelectionStart+_Text.Length-Text.Length;
Text=_Text;
SelectionStart=pos<0?0:pos;
}
}
}
}

And declare in your code textBoxOrderid as a TextBoxNum :
TextBoxNum textBoxOrderid
Hope this helps,

Ludovic SOEUR.


Thanks for all the information. I got it working the following way:

private void textBoxOrderid_TextChanged(object sender, System.EventArgs e)
{
string temp = textBoxOrderid.Text;
string newval = "";
for (int i = 0; i < temp.Length; i++)
{
if (temp[i] >= '0' && temp[i] <= '9')
{
newval += temp[i];
}
}
if (temp != newval)
{
textBoxOrderid.Text = newval;
textBoxOrderid.SelectionStart = textBoxOrderid.Text.Length;
}
}

Any remarks?

Jo.

Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.