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 }}