04. The "accumulate" and "Calculate" functions are implemented. The textbox does not allow the input of any character other than a number, but does not include the backspace key. The first value is not allowed to be 0, and the textbox does not.

Source: Internet
Author: User

04. The "accumulate" and "Calculate" functions are implemented. The textbox does not allow the input of any character other than a number, but does not include the backspace key. The first value is not allowed to be 0, and the textbox does not.

Private void button#click (object sender, EventArgs e) {double number1, number2; if (double. tryParse (txtNumber1.Text, out number1) = false) {MessageBox. show ("Incorrect start value"); txtNumber1.Focus (); txtNumber1.SelectAll (); return;} if (double. tryParse (txtNumber2.Text, out number2) = false) {MessageBox. show ("incorrect end value"); txtNumber2.Focus (); txtNumber2.SelectAll (); return;} if (number2 <number1) {MessageBox. show ("first number The value cannot be greater than the second value. Enter "); txtNumber1.Focus (); txtNumber1.SelectAll (); return;} double sum = 0; for (double I = number1; I <= number2; I ++) {sum = sum + I; I ++;} lbResult. text = sum. toString ();} private void txtNumber1_TextChanged (object sender, EventArgs e) {lbResult. text = "";} private void btnExit_Click (object sender, EventArgs e) {this. close () ;}// the content stored in sender is the corresponding control object of the event triggered by the corresponding control. An object is a parent class that can be accessed by all subclasses. Each control corresponds to a class private void txtNumber1_KeyPress (object sender, KeyPressEventArgs e) // keypress event {TextBox txtNumber1 = sender as TextBox; // if (e. keyChar <'0' | e. keyChar> '9') // The condition is used to prevent the user from entering the corresponding code in the ascii table. You can only enter numbers {e. handled = true;} // selectionstart In the textbox attribute indicates the starting position if a piece of text is selected. If not selected, it indicates the cursor position. // Note! The first position is 0if (txtNumber1.SelectionStart = 0 & e. keyChar = '0') // if the user cursor is at the beginning of the first position and enters 0, the user cannot input {e. handled = true;} if (e. keyChar = 8) // determines whether the user inputs the backspace key {e. handled = false; // cancel e. handled function to block user input }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.