TextBox Input Constraints Problem
I have input constraints on textbox that it will only accept the int and until it can be parsed by Int64. The question 开发者_Go百科is it clears out because he is assigning to String.Empty but what if its is not parsable by Int64 then i want to suppress that keypress. I tried adding code to keydown event that i have but it will also suppress the Int keys.
public static void SetInt64Input(Control tb)
{
tb.KeyDown += (sender, e) => {
if (!IsIntKeyOrAlwaysAcceptableKey(e))
//if not int key suppress it; Ex: A,B,.etc
e.SuppressKeyPress = true;
};
tb.TextChanged += (s, e) => ClearAllNonInt64Inputs(tb);
}
private static void ClearAllNonInt64Inputs(Control tb)
{
long i;
if (!Int64.TryParse(tb.Text, out i))
tb.Text = String.Empty;
else
tb.Text = Int64.Parse(tb.Text).ToString();
}
i tried doing
if(!Int64.TryParse(((TextBox)sender).Text,out junk)
e.SuppressKeyPress = true;
but it dint work.
i am not sure whats the question.sorry. But if the input is not parsable due to its out of length of Int64 (more than 4 bytes), then i think restricting the numbers of characters in the Textbox may help. And if its not parsable due to a non-integer value, then use of IsNotDigit function will be helpfull in keypress event.
精彩评论