C#计算器投射按钮

时间:2018-01-05 12:00:24

标签: c#

我在Windows窗体应用程序中使用.NET C#编写一个简单的计算器。

我把这个计算器做了3年,现在我不了解代码的某些部分而且发现了一个错误。

所以,我输入了一个按钮来读取数字按钮(?),然后我使用一个开关来了解按下的数字或操作的数量。

按钮b =(按钮)发件人

它正在发挥作用。 问题是,当我点击一个按钮(表单中的某个地方)外,它会引发异常。

任何帮助?

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
        {

        }
    }
}

}

源代码: https://pastebin.com/p1ggeSz4

异常错误是: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;
    }

2 个答案:

答案 0 :(得分:0)

如果转换失败,

Button b = (Button)sender;将抛出异常,因此仅当您希望转换始终成功时才应使用此方式。如果你改为写:  Button b = sender as Button;如果转换失败,变量b将为null,因此您可以像下面这样处理它:  if(b == null) return;

答案 1 :(得分:0)

感谢用户,我设法做到了! 谢谢你们。 这就是它的外观:

Button b = sender as Button;
if (b == null)
{
     return;
}