Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C # to realize simple calculator function

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use C # to achieve a simple calculator function, the article is very detailed, has a certain reference value, interested friends must read it!

Environment: VS2010 and above

1. Set up a Window forms application

2. Drag two TextBox from the toolbox, the first one on top and the second one on the bottom. The main Name here is textBox1 above and textBox2 below. This involves the writing of later code.

3. Drag the Button in the toolbar and place it. The alignment tool above can be used to assist the design.

4. Change the Text of each Button in the attribute, as follows

Note that the Text of 1 / 9, decimal point, ±* / should be only one character, don't type too much. ←

5. Select any Button, right-click, select View Code, and go to Form1.cs

6. Start writing code

AddNum modifies the Text of TextBox, which is applied to Click events of 1x 9 and decimal point

Reset resets temp, myoperator, and Text for both TextBox

Delete deletes the last character of textBox2's Text

Calculate converts the Text of textBox2 to double to temp, and modifies myoperator

The specific calculation of Equal

Using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApp1 {public partial class Form1: Form {public Form1 () {InitializeComponent ();} / /-above is the automatically generated code, and the following is handwritten by us-private double temp = 0 / / storing temporary data private char myoperator =''; / / judging which private void AddNum (object sender, EventArgs e) {/ / 1q9 / and the Click event / / sender of the decimal point is the control that raised the event. Here we unpack it as Button Button button = (Button) sender; textBox2.Text + = button.Text. } Click event temp = 0 for private void Reset (object sender, EventArgs e) {/ / CE; myoperator =''; textBox1.Text = textBox2.Text = "" } private void Delete (object sender, EventArgs e) {/ / Click event of ← / / remove the last character if (textBox2.TextLength > 0) textBox2.Text = textBox2.Text.Remove (textBox2.TextLength-1) } Click event Button button of private void Calculate (object sender, EventArgs e) {/ / +-* / Button button = (Button) sender; if (double.TryParse (textBox2.Text, out temp)) / / attempts to convert Text of textBox2 to double and assign a value to temp {myoperator = button.Text [0] / / Text is string, take the first character textBox1.Text = temp.ToString () +''+ myoperator; textBox2.Text = "";} else {/ / conversion failed, reset all Reset (sender, e) }} Click event of private void Equal (object sender, EventArgs e) {/ / =, calculates and displays double temp2; / / attempts to convert, resets and returns if (! double.TryParse (textBox2.Text, out temp2)) {Reset (sender, e); return if failed } switch (myoperator) {case'+': temp + = temp2; break; case'-': temp-= temp2; break; case'*': temp * = temp2 Break; case'/': temp / = temp2; break; default: break;} textBox1.Text = "; textBox2.Text = temp.ToString ();}

7. Set the Click event for each Button

AddNum: 1x 9 and Click event of decimal point

Click event of Reset:CE

Delete: Click event for ←

Click event of Calculate: ±* /

Click event of Equal:=

8. Start (F5)

The above is all the contents of the article "how to use C # to achieve simple Calculator functions". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report