I am using text box keypress
event to handle only selected inputs. Basically the textbox
allow user to input values where it can be calculated.
i.e. you can type (5*5)- (10/5)
.
The current method check like Convert.ToChar("*")==e.KeyChar
etc...
At the moment it doesn't allow user to copy paste values.
Is there anyway that can detect the ctrl+v
on keypress
event?
Update
What I am doing at the moment
static IEnumerable<char> ValidFiancialCharacters
{
get
{
if(_validFiancialCharacters==null)
{
_validFiancialCharacters = new List<char>();
_validFiancialCharacters.Add(Convert.ToChar("0"));
_validFiancialCharacters.Add(Convert.ToChar("1"));
_validFiancialCharacters.Add(Convert.ToChar("2"));
// till 9 and
_validFiancialCharacters.Add(Convert.ToChar("+"));
_validFiancialCharacters.Add(Convert.ToChar("-"));
_validFiancialCharacters.Add(Convert.ToChar("/"));
//and some other
}
return _validFiancialCharacters;
}
}
public static bool ValidateInput(KeyPressEventArgs e)
{
if (ValidFiancialCharacters.Any(chr => chr == e.KeyChar))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
return e.Handled;
}
And in the keypress
private void txtRate_KeyPress(object sender, KeyPressEventArgs e)
{
NumberExtension.ValidateInput(e);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…