C#Windows窗体应用程序无效异常问题
这是一个简单的计算器程序,我正在尝试使用VS中的Windows窗体应用程序制作。当我单击计算器按钮以外的任何位置时,都会出现未处理的异常。我是C#的新手,我的函数“common#u operators”中的sender按钮似乎导致了异常。此外,我希望此计算器具有与windows 10内置计算器类似的内存功能。我到处都搜索过,但找不到类似Win10内置计算器的c#计算器实现,我已经开始了,但我认为有更好的实现方法。如果您需要更多信息,我已经上传了与表单应用程序相关的“”文件C#Windows窗体应用程序无效异常问题,c#,winforms,C#,Winforms,这是一个简单的计算器程序,我正在尝试使用VS中的Windows窗体应用程序制作。当我单击计算器按钮以外的任何位置时,都会出现未处理的异常。我是C#的新手,我的函数“common#u operators”中的sender按钮似乎导致了异常。此外,我希望此计算器具有与windows 10内置计算器类似的内存功能。我到处都搜索过,但找不到类似Win10内置计算器的c#计算器实现,我已经开始了,但我认为有更好的实现方法。如果您需要更多信息,我已经上传了与表单应用程序相关的“”文件 using Syste
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;
using WindowsFormsApp_Calculator;
namespace WindowsFormsApp_Calculator
{
public partial class CalculatorBase : Form
{
public double[] arrMemory = new double[5];
public int indexer = 0;
double result = 0;
string asmd_operator = ""; // ASMD - Addition, Subtraction, Multiplication, Division
bool insert_value = false;
public CalculatorBase()
{
InitializeComponent();
btn_mc.Enabled = false;
btn_mr.Enabled = false;
mem_textbox.Text = "There's nothing saved in memory";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void numbers_zerotonine(object sender, EventArgs e)
{
Button b = (Button)sender;
if((box_display.Text == "0") || insert_value)
box_display.Text = "";
insert_value = false;
box_display.Text = box_display.Text + b.Text;
}
private void common_operators(object sender, EventArgs e)
{
Button b = (Button)sender;
if (result != 0)
{
btn_eql.PerformClick();
insert_value = true;
asmd_operator = b.Text;
subbox_display.Text = result + " " + asmd_operator;
}
else
{
asmd_operator = b.Text;
result = double.Parse(box_display.Text);
box_display.Text = "";
subbox_display.Text = System.Convert.ToString(result) + " " + asmd_operator;
}
}
private void btn_ce_Click(object sender, EventArgs e)
{
box_display.Text = "0";
}
private void btn_c_Click(object sender, EventArgs e)
{
box_display.Text = "0";
subbox_display.Text = "";
result = 0;
}
private void btn_eql_Click(object sender, EventArgs e)
{
subbox_display.Text = "";
switch(asmd_operator)
{
case "-":
box_display.Text = (result - double.Parse(box_display.Text)).ToString();
break;
case "+":
box_display.Text = (result + double.Parse(box_display.Text)).ToString();
break;
case "X":
box_display.Text = (result * double.Parse(box_display.Text)).ToString();
break;
case "/":
box_display.Text = (result / double.Parse(box_display.Text)).ToString();
break;
default:
break;
}
result = double.Parse(box_display.Text);
asmd_operator = "";
}
private void btn_bs_Click(object sender, EventArgs e)
{
if(box_display.Text.Length > 0)
{
box_display.Text = box_display.Text.Remove(box_display.Text.Length - 1, 1);
}
if(box_display.Text == "")
{
box_display.Text = "0";
}
}
private void btn_ms_Click(object sender, EventArgs e)
{
if (indexer == 5)
{
mem_textbox.Text = "Memory is full. Max limit = 5";
}
else
{
int hgt = 75;
mem_textbox.Text = "";
arrMemory[indexer] = double.Parse(box_display.Text);
indexer++;
btn_mc.Enabled = true;
btn_mr.Enabled = true;
TextBox mem = new TextBox();
mem.Multiline = true;
mem.TextAlign = HorizontalAlignment.Right;
mem.Width = 275;
mem.Height = 70;
mem.Font = new Font(mem.Font.FontFamily, 20);
mem.Text = box_display.Text;
mem.Location = new Point(387, hgt);
this.Controls.Add(mem);
}
}
private void btn_mc_Click(object sender, EventArgs e)
{
foreach (int i in arrMemory)
{
arrMemory[i] = 0;
}
indexer = 0;
btn_mr.Enabled = false;
btn_mc.Enabled = false;
mem_textbox.Text = "There's nothing saved in memory";
}
private void btn_mr_Click(object sender, EventArgs e)
{
box_display.Text = arrMemory[indexer].ToString();
}
private void btn_mp_Click(object sender, EventArgs e)
{
arrMemory[indexer] += double.Parse(box_display.Text);
}
private void btn_mm_Click(object sender, EventArgs e)
{
arrMemory[indexer] -= double.Parse(box_display.Text);
}
}
}
在designer.cs中,表单本身有一个调用公共_运算符的
Click
事件处理程序,因此如果触发了该事件,这将是一个无效的强制转换,因为发送方
将是您的计算器数据库
表单类型,而不是按钮
哪些控件订阅了事件常见的\u操作符
?其中一个不是按钮吗?如果您需要更多信息,我已经在desc中上传了一个链接。我该如何防止呢,先生?最简单的方法是删除designer.cs文件中的一行,该行的内容为:this.Click+=new System.EventHandler(this.common\u operators)代码>成功了。谢谢你,好心的先生。