Hey all,
I have to validate a textbox in windows forms for alphanumeric
characters such that non alphanumeric key presses are ignored.
Some help would be appreciated
Mahmoud Al-Qudsi - 30 May 2007 18:31 GMT
On May 30, 8:15 pm, kanepa...@hotmail.com wrote:
> Hey all,
>
> I have to validate a textbox in windows forms for alphanumeric
> characters such that non alphanumeric key presses are ignored.
>
> Some help would be appreciated
try isDigit and isChar functions when validating the .keydown action.
Morten Wennevik [C# MVP] - 30 May 2007 18:59 GMT
> On May 30, 8:15 pm, kanepa...@hotmail.com wrote:
>> Hey all,
[quoted text clipped - 5 lines]
>
> try isDigit and isChar functions when validating the .keydown action.
Even better,
Char.IsLetterOrDigit(char), you need to use the KeyPress event to get the characters and cancel (Handled) the event.

Signature
Happy coding!
Morten Wennevik [C# MVP]
Daniel Cigic - 30 May 2007 18:31 GMT
kanepa...@hotmail.com wrote:
> Hey all,
>
> I have to validate a textbox in windows forms for alphanumeric
> characters such that non alphanumeric key presses are ignored.
>
> Some help would be appreciated
This is not complete but something like this I gues:
You can add KeyDown event handler to your textbox ... for example:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.OemMinus) // and of course more checks
here ....
{
e.SuppressKeyPress = true;
e.Handled = true;
}
}
kanepart2@hotmail.com - 30 May 2007 18:51 GMT
> kanepa...@hotmail.com wrote:
> > Hey all,
[quoted text clipped - 3 lines]
>
> > Some help would be appreciated
I tried both the above . e does not have a property called KeyCode
> This is not complete but something like this I gues:
>
[quoted text clipped - 9 lines]
> }
> }
Peter Duniho - 30 May 2007 21:01 GMT
> I tried both the above . e does not have a property called KeyCode
Sure it does, if you are actually handling the KeyDown event which has the
KeyEventArgs parameter:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keyco
de.aspx
That said, it seems to me that Morton's advice about using the KeyPress
event instead is better. Set the e.Handled property to "true" to avoid
non-alphanumeric characters from being entered.
Also note that there's a MaskedTextBox that will do this sort of filtering
for you. If all you are trying to do is restrict input to alphanumerics,
that might be a better choice.
Pete
BLUE - 30 May 2007 18:47 GMT
//
// AlphaNumTextBox.cs
//
using System;
using System.Windows.Forms;
namespace AlphaNumericTextBox
{
/// <remarks>
/// A text box to contain alphanumeric data
/// </remarks>
internal sealed class AlphaNumTextBox : TextBox
{
#region Fields
private bool isBack = false;
private bool isAlphaNum = false;
#endregion
#region Event raisers
protected override void OnKeyDown(KeyEventArgs e)
{
Keys k = e.KeyCode;
if (k == Keys.Back)
{
this.isBack = true;
this.isAlphaNum = false;
}
else
{
bool isAlpha = !e.Alt && !e.Control && (k >= Keys.A && k <=
Keys.Z);
// Determine whether the keystroke is a number
bool d0d9 = k >= Keys.D0 && k <= Keys.D9;
bool numpad = k >= Keys.NumPad0 && k <= Keys.NumPad9;
bool isNumber = (!e.Alt && !e.Control && !e.Shift) && (d0d9
|| numpad);
this.isBack = false;
this.isAlphaNum = isAlpha || isNumber;
}
base.OnKeyDown(e);
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!this.isBack && !this.isAlphaNum)
{
e.Handled = true;
return;
}
char c = e.KeyChar;
if (this.isBack && this.SelectionStart > 0)
{
int currentPosition = SelectionStart;
this.Text = this.Text.Substring(0, this.TextLength - 1);
this.SelectionStart = currentPosition - 1;
e.Handled = true;
return;
}
if (this.isAlphaNum && this.SelectionStart < this.MaxLength)
{
int currentPosition = SelectionStart;
this.Text = this.Text.Insert(currentPosition,
Char.ToUpper(c).ToString());
this.SelectionStart = currentPosition + 1;
e.Handled = true;
return;
}
base.OnKeyPress(e);
}
#endregion
}
}
Bye,
Luigi.
Mihai N. - 31 May 2007 08:35 GMT
> Hey all,
>
> I have to validate a textbox in windows forms for alphanumeric
> characters such that non alphanumeric key presses are ignored.
Filtering at key-press is usually the bad approach.
It will mess-up a languages that have more complex input
(dead-keys, IME, etc.)

Signature
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email