繁体   English   中英

C#计算器按下按钮

[英]C# Calculator Cast a Button

我正在Windows窗体应用程序中的.NET C#中制作一个简单的计算器。

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

因此,我按下了一个Button来读取数字button(?),然后使用一个Switch来知道按下的数字或所操作的数字。

按钮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;
    }

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

感谢用户,我设法做到了! 谢谢你们。 看起来就是这样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM