I'm making a simple calculator in .NET C# in Windows Forms Application.
I made this calculator 3 years and now I don't understand some parts of the code and I found a bug.
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 b = (Button)sender
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
exception error is: 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;
The variable b will be null if the casting failed, so you can handle it like: 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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.