prevent non-numeric keystroke in textbox

  • Thread starter Thread starter Jack Fox
  • Start date Start date
J

Jack Fox

Under VB6 it was easy to prevent a non-numeric (or any kind of character)
keystroke in the KeyPress event of a TextBox, becasue you could simply set
the KeyAscii parameter to zero. The .Net Framework does not work like this.

I have found sample code to clear the entire textbox, but preventing
unwanted characters on a keystroke by keystroke basis is much more user
friendly. There must be a simple technique to do this, I just don't know
what it is.
 
In the Keypress event, using KeyPressEventArgs e

if (Char.IsDigit(e.KeyChar))
{}

HTH Pete.
 
Identification is easy, but KeyChar is ReadOnly. There doesn't seem to be
any simple solution to "erasing" unwanted keystrokes like there is in VB.



This is tedious, but it works. (Submissions of simpler solutions
appreciated.) You have to have code in 2 events because the text property is
not yet updated in KeyPress:



private bool bBadEditionNbr;

private char cEditionNbr;



private void txtEditionNbr_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)

{

if (Char.IsDigit(e.KeyChar))

bBadEditionNbr = false;

else

if(e.KeyChar != (char) 8) //check for backspace

{

bBadEditionNbr = true;

cEditionNbr = e.KeyChar;

}

}



private void txtEditionNbr_TextChanged(object sender, System.EventArgs e)

{

if (bBadEditionNbr)

{

char[] TextArray = new char[txtEditionNbr.Text.Length];

txtEditionNbr.Text.CopyTo(0, TextArray, 0, txtEditionNbr.Text.Length);

char[] NewTextArray = new char[TextArray.Length - 1];

int k = 0;

for (int i = 0; i < TextArray.Length; i++)

if (TextArray != cEditionNbr)

{

NewTextArray[k] = TextArray;

k++;

}

bBadEditionNbr = false;

txtEditionNbr.Text = new String(NewTextArray);

}

}
 
It turns out that my solution below disrpts the current positioning of the
selection point in the textbox (it get moved to 0).

..NET is a huge advance in so many ways, but in this case it seems to be a
big step backward. (Remember, in VB I could do this in one line of code!)

Jack Fox said:
Identification is easy, but KeyChar is ReadOnly. There doesn't seem to be
any simple solution to "erasing" unwanted keystrokes like there is in VB.



This is tedious, but it works. (Submissions of simpler solutions
appreciated.) You have to have code in 2 events because the text property is
not yet updated in KeyPress:



private bool bBadEditionNbr;

private char cEditionNbr;



private void txtEditionNbr_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)

{

if (Char.IsDigit(e.KeyChar))

bBadEditionNbr = false;

else

if(e.KeyChar != (char) 8) //check for backspace

{

bBadEditionNbr = true;

cEditionNbr = e.KeyChar;

}

}



private void txtEditionNbr_TextChanged(object sender, System.EventArgs e)

{

if (bBadEditionNbr)

{

char[] TextArray = new char[txtEditionNbr.Text.Length];

txtEditionNbr.Text.CopyTo(0, TextArray, 0, txtEditionNbr.Text.Length);

char[] NewTextArray = new char[TextArray.Length - 1];

int k = 0;

for (int i = 0; i < TextArray.Length; i++)

if (TextArray != cEditionNbr)

{

NewTextArray[k] = TextArray;

k++;

}

bBadEditionNbr = false;

txtEditionNbr.Text = new String(NewTextArray);

}

}


trinitypete said:
In the Keypress event, using KeyPressEventArgs e

if (Char.IsDigit(e.KeyChar))
{}

HTH Pete.
 
Under VB6 it was easy to prevent a non-numeric (or any kind of character)
keystroke in the KeyPress event of a TextBox, becasue you could simply set
the KeyAscii parameter to zero. The .Net Framework does not work like this.

I have found sample code to clear the entire textbox, but preventing
unwanted characters on a keystroke by keystroke basis is much more user
friendly. There must be a simple technique to do this, I just don't know
what it is.

What you do is handle the textbox character processing in the
KeyPress event, discarding the characters you don't want.

In the following example I have a textbox called textbox3 that
will accept any input except for the character 1 (one):

private void textBox3_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == '1') e.Handled = true;
}

Does that answer your question?

Oz
 
Back
Top