【问题标题】:C# Calculator - Can't insert a number before a decimal pointC#计算器 - 不能在小数点前插入数字
【发布时间】:2019-03-21 10:07:50
【问题描述】:

我已经在 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 = ".";
            }

        }
    }
}

提前致谢:)

【问题讨论】:

    标签: c# .net visual-studio calculator


    【解决方案1】:

    最后一行:

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

    第二个= 应该是+

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

    【讨论】:

      【解决方案2】:

      作为NibblyPig's answer的补充:

      为什么要创建一个新的Button 以及为什么要将该按钮的.Text(它是string.Empty,因为它尚未设置)添加到txtResult.Text,而它已经包含一个点?

      优化后的版本是:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-21
        • 2022-06-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多