I'm trying to set a hex mask for a textbox. So that only valid hex numbers can be entered. (And ',' and 'ENTER')
It almost works. So far it will only allow small letters from a-f and numbers 0-9, but I can still enter capital letters GHIJKLM. (At first, when program is started it seems to accept one char ex k, but after it has excepted k once it wont be shown after that, until next time you start the program. That's weird.)
Here is a part of code:
private void EnterKey(Object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// if keychar == 13 is the same as check for if <ENTER> was pressed
if (e.KeyChar == (char)13)
{
// is <ENTER> pressed, send button_click
button1_Click(sender, e);
}
{
// this will only allow valid hex values [0-9][a-f][A-F] to be entered. See ASCII table
char c = e.KeyChar;
if (c != '\b' && !((c <= 0x66 && c >= 61) || (c <= 0x46 && c >= 0x41) || (c >= 0x30 && c <= 0x39) || (c == 0x2c)))
{
e.Handled = true;
}
}
}
This is how I bind the event:
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyDown);
}
Could anyone of you wise guys, see what I'm doing wrong?
It's my first small program, so go easy on me :o)
This:
c <= 0x66 && c >= 61
Should be:
c <= 0x66 && c >= 0x61 //hex literal
Note that you're wasting valuable time by looking up hex codes, you can easily compare characters:
if ((c >= 'a') && (c <= 'f'))
As for the first character: you shouldn't bind the KeyPress at the TextChanged event - it is too late! Here's the sequence of events:
Form Loads
...
User click on a key.
TextChanged triggered, changing the text and binding the event.
User click on a key.
KeyPress triggered.
What you want to do is to bind the event right from the start. The best place is the Form_Load event.
You can also use the Properties window to bind the event at design time
If you had not used magic numbers, you would never have run into this problem. Rewrite your if like this:
if (!(c == '\b' || ('0' <= c && c <= '9') || ('A' <= c && c <= 'F') // et cetera
Use a regular expression:
using System.Text.RegularExpressions;
...
if (!(Regex.IsMatch(e.KeyChar.ToString(), "^[0-9a-fA-F]+$"))
e.Handled = true;
What type of program is this an ASP.NET website or some type of winforms/wpf thick client? The reason I ask is that you may be testing on stale code. Otherwise on change can be to just flip the checking logic to be more aligned with what you want. Ensuring that the entered character is one element in the allowed set. A refactoring is below.
e.Handled = (c >= 0x61 && c <=0x66) || (c >=0x41 && c<= 0x46) || (c >= 0x30 && c <= 0x39);
As an alternative approach if you just want to validate the whole textbox at one time instead of after each key press you can just parse the value to see if it is a number. The following code fragment will generate parse the value 11486525 from AF453d. If the number is not a valid hex value then the result of isHex will be false;
int i;
string s = "AF453d";
bool isHex;
isHex = int.TryParse(s, System.Globalization.NumberStyles.AllowHexSpecifier, null, out i);
Console.WriteLine(isHex);
Console.WriteLine(i);
Why complicate it?
private void EnterKey(Object sender, System.Windows.Forms.KeyPressEventArgs e)
{
char c = e.KeyChar;
e.Handled = !System.Uri.IsHexDigit(e.KeyChar) && c != 0x2C;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
/* less than 0 or greater than 9, and
* less than a or greater than f, and
* less than A or greater than F, and
* not backspace, and
* not delete or decimal (which is the same key as delete). */
if (
((e.KeyChar < 48) || (e.KeyChar > 57)) &&
((e.KeyChar < 65) || (e.KeyChar > 70)) &&
((e.KeyChar < 97) || (e.KeyChar > 102)) &&
(e.KeyChar != (char)Keys.Back) &&
((e.KeyChar != (char)Keys.Delete) || (e.KeyChar == '.'))
) e.Handled = true;
}
Based on the answer by Kobi for WPF
private void EnterKey(Object sender, KeyEventArgs e)
{
Key c = e.Key;
if (!((c >= Key.A) && (c <= Key.F)))
{
if (!((c >= Key.D0) && (c <= Key.D9)))
{
if (!((c >= Key.NumPad0) && (c <= Key.NumPad9)))
{
e.Handled = true;
LogText("Handled");
}
}
}
}
Captures letters, numbers and keypad numbers.
Related
I want to validate a Textbox for a specific input pattern that contains Number, dot & plus singh only.
for example.
50.4+50.6+60.7+80.4
etc...
I want user can input only in this pattern because at last I want to plus this all value separated by plus singh. So it is necessary for a user that he follow this pattern.
please any body give me solution for this.
I am working in c# Windows form application.
Using a KeyPress event:
private void CheckInput(object sender, KeyPressEventArgs e)
{
// Make sure only digits, . and +
if (!char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != '+')
{
e.Handled = true;
}
// Make sure . is in correct places only
else if (e.KeyChar == '.')
{
for (int i = textBox1.SelectionStart - 1; i >= 0; i--)
{
if (textBox1.Text[i] == '.')
{
e.Handled = true;
break;
}
else if (textBox1.Text[i] == '+') break;
}
}
// Make sure character before + is a digit
else if (e.KeyChar == '+'
&& !char.IsDigit(textBox1.Text[textBox1.SelectionStart - 1]))
{
e.Handled = true;
}
}
I am working on a database application and using this class to validate numeric numbers on KeyPress event of TextBox.
The numbers may have (-) negative values with fixed decimal places (third parameter dPlaces) e.g. 10000, -1000, 12345.45, -12345.45
After adding a decimal, I am not able to edit other digits although without a decimal it is working perfectly.
Thanks in advance
public static class Util
{
public static void NumInput(object sender, KeyPressEventArgs e, int dPlaces)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.' && (e.KeyChar != '-'))
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
var a = (sender as TextBox).SelectionLength;
// only allow minus sign at the beginning
var x = (sender as TextBox).Text.IndexOf('-');
if (e.KeyChar == '-' && (sender as TextBox).Text.IndexOf('-') > 0)
{
e.Handled = true;
}
if (!char.IsControl(e.KeyChar))
{
TextBox textBox = (TextBox)sender;
if (textBox.Text.IndexOf('.') > -1 &&
textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >= dPlaces + 1)
{
e.Handled = true;
}
}
}
}
Its because of Logical operation in your IF BLOCK for comparing length =3 and char = '.'.
Change that last part of your code with : (EDIT : To handle the issue of inserting text before '.')
if (!char.IsControl(e.KeyChar))
{
TextBox textBox = (TextBox)sender;
// get position of new char to be inserted
int position = textBox.SelectionStart;
if (textBox.Text.IndexOf('.') > -1 && position > textBox.Text.IndexOf('.')) // check location of new char
if(!(textBox.Text.Substring(textBox.Text.IndexOf('.')).Length <= dPlaces + 1))
{
e.Handled = true;
}
}
This will do your job..!!!
EDIT : Also Do the following to STOP Copy/Past in textbox
textbox.ShortcutsEnabled = false;
I created a textbox dynamically with a TextChangedEventArgs to restrict the textbox to enter only numbers and decimal point.
Following is the code in c#
const char Delete = (char)8;
if (Char.IsDigit(e.KeyChar))
{
e.Handled = false;
}
else if (e.KeyChar == Delete)
{
e.Handled = false;
}
else if (e.KeyChar == '.')
{
if (!(amt.Text.Contains(".")))
e.Handled = false;
else
{
e.Handled = true;
}
}
else
{
e.Handled = true;
}
But I can't use this in wpf.
I tried to change the code with e.key or e.Text. But both these are not available. It is showing the following error Are u missing an assembly or directive.
Please anyone help me.
// one solution for filtering characters in a textbox.
// this is the PreviewKeyDown handler for a textbox named tbNumerical
// Need to add logic for cancelling repeated decimal point and minus sign
// or possible notation like 1.23e2 == 123
private void tbNumerical_PreviewKeyDown(object sender, KeyEventArgs e)
{
System.Windows.Input.Key k = e.Key;
// to see the key enums displayed, use a textbox or label
// someTextBox.Text = k.ToString();
// filter out control keys, not all are used, add more as needed
bool controlKeyIsDown = Keyboard.IsKeyDown(Key.LeftShift);
if (!controlKeyIsDown &&
Key.D0 <= k && k <= Key.D9 ||
Key.NumPad0 <= k && k <= Key.NumPad9 ||
k == Key.OemMinus || k == Key.Subtract ||
k == Key.Decimal || k == Key.OemPeriod) // or OemComma for european decimal point
else
{
e.Handled = true;
// just a little sound effect for wrong key pressed
System.Media.SystemSound ss = System.Media.SystemSounds.Beep;
ss.Play();
}
}
I'd like to process an alphabetic character and underscores. How do I tell what char is typed if a SHIFT is also pressed. Currently, shifted chars are handled by the ELSE clause.
private void txtSearch_KeyUp(object sender, KeyEventArgs e)
{
if (((int)e.KeyData >= 65 && (int)e.KeyData <= 122) ||
(e.KeyData.ToString() == "_"))
{
System.Diagnostics.Debug.WriteLine(e.KeyData);
//char thisChar = char excluding SHIFT, Control
System.Diagnostics.Debug.WriteLine("Process " + thisChar);
}
else
{
System.Diagnostics.Debug.WriteLine("Throw away a " + e.KeyData);
}
}
if (e.Shift && (((int)e.KeyData >= 65 && (int)e.KeyData <= 122) || (e.KeyData.ToString() == "_")))
{
//Code here
}
G'day,
I took a slight different approach:
private void txtSearch_KeyUp(object Sender, KeyEventArgs E)
{
int iKeyData = (int)(E.KeyData);
if (((E.KeyData.HasFlag(Keys.OemMinus) == true) && (E.Shift == true)) || ((iKeyData >= 65) && (iKeyData <= 122)))
{
System.Diagnostics.Debug.WriteLine(E.KeyData);
}
else
{
System.Diagnostics.Debug.WriteLine("Throw away a " + E.KeyData);
}
}
I'm not sure how this will work with different keyboard layouts though - might want to look into that too.
Cheers!
I remember using some attribute on the getter/setter that would limit the input to a certain datatype, length etc. IE [Attribute something something].
Any ideas?
Thanks
Did you mean the ValidateInput attribute available in System.Web.Mvc?
Also, you could probably use a MaskedTextBox if you're doing WinForms.
One way to do it (if you want to keep using a standard text box) would be to make an event for the text changed event of the text box, and in that, read the text to make sure that it contains only numbers (and an optional period in the case of a double)
Winforms? Have you considered using masked Textbox control?
http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx
Winforms? Why not use a NumericUpDown?
http://msdn.microsoft.com/en-us/library/57dy4d56.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.aspx
If you need scientific notation you can do :
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
TextBox tBox = (TextBox)sender;
if (!((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)
|| (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
|| (e.KeyCode == Keys.Decimal && !(tBox.Text.Contains('.'))
&& !(tBox.Text.Length == 0)
&& !((tBox.Text.Length == 1)
&& (tBox.Text.Contains('-') || tBox.Text.Contains('+'))))
|| (e.KeyCode == Keys.OemPeriod && !(tBox.Text.Contains('.'))
&& !(tBox.Text.Length == 0)
&& !((tBox.Text.Length == 1)
&& (tBox.Text.Contains('-') || tBox.Text.Contains('+'))))
|| (e.KeyCode == Keys.Subtract && ((tBox.Text.Length == 0) ||
tBox.Text.EndsWith("e") || tBox.Text.EndsWith("E")))
|| (e.KeyCode == Keys.OemMinus && ((tBox.Text.Length == 0) ||
tBox.Text.EndsWith("e") || tBox.Text.EndsWith("E")))
|| (e.KeyCode == Keys.Add && ((tBox.Text.Length == 0) ||
tBox.Text.EndsWith("e") || tBox.Text.EndsWith("E")))
|| (e.KeyCode == Keys.Oemplus && ((tBox.Text.Length == 0) ||
tBox.Text.EndsWith("e") || tBox.Text.EndsWith("E")))
|| e.KeyCode == Keys.Delete
|| e.KeyCode == Keys.Back
|| e.KeyCode == Keys.Left
|| e.KeyCode == Keys.Right
|| (e.KeyCode == Keys.E) && !(tBox.Text.Contains('e')) &&
(tBox.Text.Contains('.') && !tBox.Text.EndsWith("."))))
{
e.SuppressKeyPress = true;
}
}
This will deny input of any pattern which is not consistent with a numeric value. Minus signs are only allowed at the beginning of the string (to indicate a negative number) and after an e or E to indicate a negative exponent. Plus signs follow the same rule as minus. Only one decimal point is allowed and it must follow at least one number. Only one e or E is allowed and it must follow a decimal point and at least one number after the decimal point.
You could also allow things like the Help, Tab, etc, keys if it would interfere with other aspects of your program function.
Note that this does not prevent incomplete numbers (ie: 1.37E- or -13. from being entered so you would probably want to check the string in any case. This at least denies any immediately invalid entries.
You might do something like (in the same handler, before the other logic):
if (e.KeyCode == Keys.Enter)
{
textBox1_Validating(sender, new CancelEventArgs());
return;
}
The above only gives the enter key the normal 'feel' for input (force validation). Leaving the textbox (going out of focus) will also trigger validation where you might do something like :
private void textBox1_Validating(object sender, CancelEventArgs e)
{
TextBox tBox = (TextBox)sender;
double tstDbl;
if (!double.TryParse(tBox.Text, out tstDbl))
{
//handle bad input
}
else
{
//double value OK
doSomething(tstDbl);
}
}