winform计算器退格
时间: 2023-10-03 10:08:05 浏览: 51
winform计算器退格的实现方法是通过捕捉退格按钮的点击事件,在事件处理方法中删除当前显示结果中的最后一个字符。具体的实现代码可以参考以下示例:
private void butback_Click(object sender, EventArgs e)
{
if (i == 1)
{
if (txt1.Text.Length > 0)
{
txt1.Text = txt1.Text.Substring(0, txt1.Text.Length - 1);
}
}
else if (i == 2)
{
if (txt3.Text.Length > 0)
{
txt3.Text = txt3.Text.Substring(0, txt3.Text.Length - 1);
}
}
}
在上述代码中,使用了条件判断来确定当前是第一个操作数还是第二个操作数,然后通过Substring方法删除相应文本框中的最后一个字符。
相关问题
winform计算器历史记录
对于WinForm计算器的历史记录,可以考虑使用一个文本框(TextBox)来显示用户的输入和计算历史。每次用户进行计算时,将计算表达式和结果追加到文本框中。
以下是一个简单的示例代码:
1. 在窗体上添加一个名为textBoxHistory的TextBox控件,设置其Multiline属性为True,以显示多行文本。
2. 在窗体的构造函数中初始化历史记录文本框:
```csharp
public partial class CalculatorForm : Form
{
public CalculatorForm()
{
InitializeComponent();
textBoxHistory.Text = "";
}
}
```
3. 在计算按钮的点击事件处理程序中,将用户输入和计算结果追加到历史记录文本框中:
```csharp
private void buttonCalculate_Click(object sender, EventArgs e)
{
string expression = textBoxExpression.Text;
double result = Calculate(expression);
// 追加到历史记录
textBoxHistory.AppendText($"{expression} = {result}\r\n");
// 清空表达式输入框
textBoxExpression.Text = "";
}
```
这样,每次用户点击计算按钮时,计算表达式和结果就会显示在历史记录文本框中。用户就可以看到他们的计算历史了。需要注意的是,Calculate函数是自定义的用于执行计算的方法,你需要根据你的具体需求来实现。
希望这个简单示例能够帮助到你!如果有任何问题,请随时提问。
winform写计算器
WinForm是Windows Forms的简称,是一种基于Windows操作系统的图形用户界面应用程序框架。通过WinForm,可以很方便地进行Windows平台下的界面设计与开发。
下面是一个简单的WinForm计算器实现:
1. 新建一个WinForm应用程序项目;
2. 在设计视图中拖入按钮和文本框等控件,设计界面;
3. 在代码视图中编写事件处理程序,实现计算器功能;
4. 可以使用DataTable.Compute()方法来实现计算器的核心功能,也可以通过编写表达式来实现计算器功能。
以下是一个简单的WinForm计算器的示例代码:
```
using System;
using System.Data;
using System.Windows.Forms;
namespace Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
txtResult.Text += btn.Text;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtResult.Clear();
}
private void btnEquals_Click(object sender, EventArgs e)
{
try
{
DataTable dt = new DataTable();
var result = dt.Compute(txtResult.Text, "");
txtResult.Text = result.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)