Replacing keyboard input in textbox

  • Thread starter Thread starter Jo Segers
  • Start date Start date
J

Jo Segers

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,
 
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
 
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.
 
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,

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
 
Cor Ligthert schreef:
Jo,

Is it not something more as this that you want.




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




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.
 
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.
 
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 >= '0' && temp <= '9')
{
newval += temp;
}
}
if (temp != newval)
{
textBoxOrderid.Text = newval;
textBoxOrderid.SelectionStart = textBoxOrderid.Text.Length;
}
}

Any remarks?

Jo.
 
OK, that's nearly the same but you should keep Regex instead of your loop if
you need efficiency.

Jo Segers said:
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 >= '0' && temp <= '9')
{
newval += temp;
}
}
if (temp != newval)
{
textBoxOrderid.Text = newval;
textBoxOrderid.SelectionStart = textBoxOrderid.Text.Length;
}
}

Any remarks?

Jo.
 
Back
Top