Record: calculator Version 0.1

Source: Internet
Author: User

Calculator? Not to mention. There is only one small function to sum All integers between two positive integers.

There are three small requirements:

1. No matter where the cursor is, press the Enter key to calculate

2. Users can only enter numbers. Other characters are not allowed.

3. The input character cannot be 0.

You only need to know some text boxes, buttons, and form attributes. Record them here.

First, create a form that meets the conditions.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192T31N7-0.png "title =" Capture. PNG "alt =" 115705601.png"/>

Set Properties for the preceding control.

Write code in the Form class for setting:

Private void btncalcu_Click (object sender, EventArgs e) {int start, end; // determines whether the content in the text box can be converted to a number. if not, the user is prompted to modify if (! Int.TryParse(this.txt start. Text, out start) {MessageBox. Show ("the start number is incorrect. Please input it again! "); // Let the current text box get the cursor this.txt start. Focus (); // select the current text box this.txt start. SelectAll (); return;} if (! Int.TryParse(this.txt end. Text, out end) {MessageBox. Show ("incorrect end number input. Please input it again! "); This.txt end. focus (); this.txt end. selectAll (); return;} int sum = 0; for (int I = start; I <= end; I ++) {sum + = I;} this.txt result. text = sum. toString ();} private void btncancel_Click (object sender, EventArgs e) {this. close ();} private void txtstart_KeyPress (object sender, KeyPressEventArgs e) {// convert the parent class object back to subclass forced conversion or as conversion) TextBox tb = sender as TextBox; // if the currently entered character is not a number between 0 and 9, the user cannot enter if (e. keyChar <'0' | e. keyChar> '9') {// when the value of Handled is true, the input e is blocked. handled = true;} // The cursor position of the text box is at 0, and the character currently entered by the user is also 0. Therefore, if (tb) is also blocked for a long time. selectionStart = 0 & e. keyChar = '0') {e. handled = true;} // The ACICAL code value of backspace is 8, and the above limit prevents the input of the backspace key if (e. keyChar = 8) {// enables the user to enter the backspace key e. handled = false ;}}

P.S:For two classes with inheritance relationships, the subclass can be converted to the parent class by default, and the conversion of the parent class to the subclass requires strong conversion. When the parent class is an object, you can use as to convert the Child class as parent class]

Double-click the button to trigger the event.

Private void txtstart_KeyPress (object sender, KeyPressEventArgs e) {} has two parameters. The sender contains the object of the control of the departure event (any control can be triggered ). KeyPressEventArgs e contains important attributes related to this event.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192T32P6-1.gif "alt =" j_0047.gif "/> Ajax Girl, come on! 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/192T32P6-1.gif "alt =" j_0047.gif "/>


This article is from the "Ajax girl" blog!

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.