In the custom textbox Control, if only numbers are allowed, consider the following three situations:
Characters entered by normal buttons, including Spanish and Chinese characters
Press the keyboard shortcut key to paste the text, that is, ctrl + v.
Click the mouse operation in the context-related menu to paste the text, that is, the "Paste" operation.
Most of the articles of the same type have considered only 1st cases and ignored 2nd and 3 common operations.
Only decimal places are allowed.
Private sub text=keypress (keyascii as integer)
If keyascii <48 or keyascii> 57 then
If keyascii = 46 then
If text1.text = "" or instr (1, text1.text, ".") <> 0 then
Keyascii = 0
Else
Keyascii = 46
End if
Else
Keyascii = 0
End if
End if
End sub
Add this button
Private void button#click (object sender, eventargs e)
{
// Add the code to implement Addition
// Add a judgment Statement by yourself, and set the input value to a number. No value is written here.
Int sum = 0;
Int number1 = int. parse (textbox1.text );
Int number2 = int. parse (textbox2.text );
Sum = number1 + number2;
Textbox3.text = sum. tostring ();
}
Only numbers and decimal places can be entered in textbox.
/// <Summary>
/// Only numbers and decimal places can be entered in textbox
/// Generally called in keypress of textbox
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Public static void textbox_keypress (object sender, keypresseventargs e)
{
If (! (E. keychar> = '0') & (e. keychar <= '9') | e. keychar <= 31 ))
{
If (e. keychar = '.')
{
If (textbox) sender). text. trim (). indexof ('.')>-1)
E. handled = true;
}
Else
E. handled = true;
}
Else
{
If (e. keychar <= 31)
{
E. handled = false;
}
}
}
The pre-processing mode of textbox Control Input, that is, shielding non-numeric keys while inputting characters. In practical applications, the post-processing mode is generally adopted, that is, input post-processing is performed in the exit and validate events of the textbox Control -- verification is performed when the control is left. However, the post-event processing mode has the following shortcomings:
When binding to a data source, inputting non-numeric characters may throw an exception. You need to consider exception capture.
Data needs to be judged and error prompts must be provided.