[英]C# Calculator Cast a Button
I'm making a simple calculator in .NET C# in Windows Forms Application. 我正在Windows窗体应用程序中的.NET C#中制作一个简单的计算器。
I made this calculator 3 years and now I don't understand some parts of the code and I found a bug. 我做了3年的这个计算器,现在我不了解代码的某些部分,但发现了一个错误。
So, I casted a Button to read the numbers buttons(?) and then I use a Switch in order to know what was the number pressed or operation. 因此,我按下了一个Button来读取数字button(?),然后使用一个Switch来知道按下的数字或所操作的数字。
Button b = (Button)sender 按钮b =(按钮)发送器
It is working. 这是工作。 The problem is that when i click outside a button (somewhere in the form) it throws a exception. 问题是,当我在按钮外(窗体中的某处)单击时,它将引发异常。
Any help? 有什么帮助吗?
using System;
using System.Windows.Forms;
namespace ex8CalculadoraCompleta
{
public partial class Form1 : Form
{
double value = 0;
string operation = "";
bool operation_pressed = false;
public Form1()
{
InitializeComponent();
}
private void btn_nclick(object sender, EventArgs e)
{
if ((txt_resultado.Text == "0")||(operation_pressed))
{
txt_resultado.Clear();
}
operation_pressed = false;
Button b = (Button)sender;
if (b.Text == ",") // Avalia se pode acrescentar outra vírgula/ponto
{
if (!txt_resultado.Text.Contains(","))
txt_resultado.Text = txt_resultado.Text + b.Text;
}
else
txt_resultado.Text = txt_resultado.Text + b.Text;
}
private void btn_ce_Click(object sender, EventArgs e)
{
txt_resultado.Text = "0"; //L
}
private void btn_operatorclick(object sender, EventArgs e)
{
Button b = (Button)sender;
if (value != 0)
{
btn_resultado.PerformClick();
operation_pressed = true;
operation = b.Text;
lbl_equation.Text = value + " " + operation;
}
else
{
operation = b.Text;
value = Double.Parse(txt_resultado.Text);
operation_pressed = true;
lbl_equation.Text = value + " " + operation;
}
//
}
private void btn_resultado_Click(object sender, EventArgs e)
{
lbl_equation.Text = "";
switch(operation) //C
{
case "+":
txt_resultado.Text = (value + Double.Parse(txt_resultado.Text)).ToString();
break;
case "-":
txt_resultado.Text = (value - Double.Parse(txt_resultado.Text)).ToString();
break;
case "*":
txt_resultado.Text = (value * Double.Parse(txt_resultado.Text)).ToString();
break;
case "/":
txt_resultado.Text = (value / Double.Parse(txt_resultado.Text)).ToString();
break;
default:
break;
} //fim switch
value = Double.Parse(txt_resultado.Text); //Convert txt Double
operation = "";
}
private void btn_c_Click(object sender, EventArgs e)
{
txt_resultado.Text = ""; //L
value = 0;
lbl_equation.Text = "";
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 48 && e.KeyChar <= 111)
{
switch (e.KeyChar.ToString())
{
case "0":
btn_0.PerformClick();
break;
case "1":
btn_1.PerformClick();
break;
case "2":
btn_2.PerformClick();
break;
case "3":
btn_3.PerformClick();
break;
case "4":
btn_4.PerformClick();
break;
case "5":
btn_5.PerformClick();
break;
case "6":
btn_6.PerformClick();
break;
case "7":
btn_7.PerformClick();
break;
case "8":
btn_8.PerformClick();
break;
case "9":
btn_9.PerformClick();
break;
case "+":
btn_soma.PerformClick();
break;
case "-":
btn_sub.PerformClick();
break;
case "*":
btn_mult.PerformClick();
break;
case "/":
btn_div.PerformClick();
break;
case "#3Dh":
btn_resultado.PerformClick();
break;
default:
break;
}
}
else
{
}
}
}
} }
Source Code: https://pastebin.com/p1ggeSz4 源代码: https : //pastebin.com/p1ggeSz4
exception error is: System.InvalidCastException 异常错误是:System.InvalidCastException
private void btn_nclick(object sender, EventArgs e)
{
if ((txt_resultado.Text == "0")||(operation_pressed))
{
txt_resultado.Clear();
}
operation_pressed = false;
Button b = (Button)sender;
if (b.Text == ",") // Avalia se pode acrescentar outra vírgula/ponto
{
if (!txt_resultado.Text.Contains(","))
txt_resultado.Text = txt_resultado.Text + b.Text;
}
else
txt_resultado.Text = txt_resultado.Text + b.Text;
}
Button b = (Button)sender;
will throw an exception if the casting failed, so casting this way should only be used when you expect the casting to always succeed. 如果强制转换失败,将抛出异常,因此仅当您希望强制转换始终成功时才应使用这种方式进行强制转换。 If you instead write: Button b = sender as Button;
如果您改写: Button b = sender as Button;
The variable b will be null if the casting failed, so you can handle it like: if(b == null) return;
如果强制转换失败,变量b将为null,因此您可以按以下方式处理它: if(b == null) return;
I've managed to do it thanks to user! 感谢用户,我设法做到了! Thank you all. 谢谢你们。 That's how it looks: 看起来就是这样:
Button b = sender as Button;
if (b == null)
{
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.