繁体   English   中英

C#计算器-无法在小数点前插入数字

[英]C# Calculator - Can't insert a number before a decimal point

我已经在C#中实现了一个计算器,除此之外一切正常,基本上,如果我尝试在小数点前输入数字,则会重置数字,然后才让我在小数点后输入数字,我认为这有点愚蠢。会很快解决,但我没有运气

这是我的代码:

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GrantCalculator
{
    public partial class Form1 : Form
    {
        int count = 0;

        float result = 0;
        string operation = "";
        bool operationPressed = false;

        public Form1()
        {
            InitializeComponent();
        }      

        private void btnClear_Click(object sender, EventArgs e)
        {
            //clearing result
            txtResult.Text = "0";
            result = 0;
        }

        private void btn_Click(object sender, EventArgs e)
        {
            if (count == 0)
            {
                //remove extra 0 
                if ((txtResult.Text == "0") || (operationPressed))
                {
                    txtResult.Clear();
                }
            }
           else if(count==1)
            {
                txtResult.Clear();
            }

            //event handler set for all  number buttons which goto result textbox
            operationPressed = false;
            Button b = (Button)sender;
            txtResult.Text += b.Text;
            count = 0;
        }

        private void operator_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            operation = b.Text;
            result = float.Parse(txtResult.Text);
            operationPressed = true;
           // count = 0;
        }

        private void btnEquals_Click(object sender, EventArgs e)
        {
            if (float.Parse(txtResult.Text) == 0 && operation == "/")
            {
                MessageBox.Show("You can't divide by 0");
            }

            //The math
            switch (operation)
                {
                    case "+":
                        txtResult.Text = (result + float.Parse(txtResult.Text)).ToString();
                        break;

                    case "-":
                        txtResult.Text = (result - float.Parse(txtResult.Text)).ToString();
                        break;

                    case "*":
                        txtResult.Text = (result * float.Parse(txtResult.Text)).ToString();
                        break;

                    case "/":
                        txtResult.Text = (result / float.Parse(txtResult.Text)).ToString();
                        break;

                    case "%":
                        txtResult.Text = (result % float.Parse(txtResult.Text)).ToString();
                        break;
                    default:
                        txtResult.Text = "Invalid";
                        break;
                }
                count++;


        }

        private void txtResult_KeyPress(object sender, KeyPressEventArgs e)
        {
            char ch = e.KeyChar;
            if (!Char.IsDigit(ch) && ch != 8 && ch != '.')
            {
                e.Handled = true;
            }

            // to allow only one decimal
            if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
            {
                e.Handled = true;
            }

        }

        private void btnPoint_Click(object sender, EventArgs e)
        {
            Button b = new Button();

            string dot = txtResult.Text;

            if (dot.Contains("."))
            {
                txtResult.Text = txtResult.Text + b.Text;
            }
            else
            {
                txtResult.Text = txtResult.Text = ".";
            }

        }
    }
}

提前致谢 :)

最后一行:

txtResult.Text = txtResult.Text = ".";

第二个=应该是+

a = b = c = d = 5; 表示法有效,并将所有设置为5

作为NibblyPig答案的补充:

为什么要创建一个新的Button ,为什么要在已经包含点的txtResult.Text添加该按钮的.Text (由于尚未设置,所以为string.Empty)?

优化版本为:

private void btnPoint_Click(object sender, EventArgs e)
{
    if (!txtResult.Text.Contains("."))
    {
        txtResult.Text += ".";
    }
}

暂无
暂无

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

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