前言:環(huán)境是vs 2022
1、打開vs2022后,右邊導(dǎo)航欄選擇創(chuàng)建新項目。
?2、選擇Windows窗體應(yīng)用(.net? Framework)
?3、進入配置新項目界面(項目名稱和位置可自行修改)點擊創(chuàng)建
?4、窗體From1即為我們要要編輯的位置
?5、在窗體中添加對應(yīng)的工具
6、并在對應(yīng)的屬性窗口為其修改對應(yīng)的屬性
以按鈕1為例可修改Name為b1(方便在代碼界面查錯)? text為1?
?
?7、以下為代碼界面內(nèi)容
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 lzk_WindowsFormsApp2
{
public partial class Form1 : Form
{
Boolean flag = true;
double x = 0;
double y = 0;
bool dot = false;
string sign = "";
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)//為c處按鈕
{
textBox1.Text = "0";
x = y = 0;
flag = true;
dot = false;
sign = "";
}
private void button1_Click(object sender, EventArgs e)//為ce處代碼
{
if(flag == false)
{
textBox1.Text = "";
dot = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void BackSpase_Click(object sender, EventArgs e)
{
if (flag == true) return;
if (textBox1.Text.EndsWith("."))
dot = false;
string s = textBox1.Text;
textBox1.Text = s.Remove(s.Length - 1);
if (textBox1.Text.Length == 0 || textBox1.Text.Equals("-"))
textBox1.Text = "";
}
private void b1_Click(object sender, EventArgs e)
{
Button b = sender as Button;
if (flag == true || textBox1.Text.Equals("o"))
{
textBox1.Text = b.Text;
flag = false;
}
else
textBox1.Text += b.Text;
}
private void jia_Click(object sender, EventArgs e)
{
Button bt = sender as Button;
flag = true;
dot = false;
if (textBox1.Text.EndsWith("."))
{//如果最后一位是小數(shù)點,則刪除
String s = textBox1.Text;
textBox1.Text = s.Remove(s.Length - 1);
}
x = double.Parse(textBox1.Text);
sign = bt.Text;//保存計算符號
}
private void bdot_Click(object sender, EventArgs e)
{
if (dot == true) return;
if (flag == true)
{
textBox1.Text = "0";
flag = false;
}
else textBox1.Text += ".";
dot = true;
}
private void deng_Click(object sender, EventArgs e)
{
if (sign.Equals("") || flag == true) return;
flag = true;
dot = false;
if (textBox1.Text.EndsWith("."))
{
string s = textBox1.Text;
textBox1.Text = s.Remove(s.Length - 1);
}
y = double.Parse(textBox1.Text);
switch (sign)
{
case "+": x = y + x; break;
case "-": x = x - y; break;
case "*": x = x * y; break;
case "/": x = x / y; break;
}
textBox1.Text = x.ToString();
sign = "";
}
private void neg_Click(object sender, EventArgs e)
{
if(flag == true || textBox1.Text.Equals("0")) return;
if (textBox1.Text.Contains("-"))
textBox1.Text = textBox1.Text.Substring(1);
else textBox1.Text = "-" + textBox1.Text;
}
}
}
8、其他的3456789按鈕可以按住shift全部選中在右邊屬性窗口選擇事件,在事件欄中的Click中選擇繼承b1的按鈕處的代碼。
文章來源:http://www.zghlxwxcb.cn/news/detail-715753.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-715753.html
到了這里,關(guān)于C# 制作簡易計算器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!