簡體   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