Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C计算器-不能在小数点前插入数字_C#_.net_Visual Studio_Calculator - Fatal编程技术网

C# C计算器-不能在小数点前插入数字

C# C计算器-不能在小数点前插入数字,c#,.net,visual-studio,calculator,C#,.net,Visual Studio,Calculator,我已经用C语言实现了一个计算器,除了这个以外,一切都很好,基本上如果我尝试在小数点之前输入一个数字,那么这个数字就会被重置,只有在小数点之后才允许我输入数字。我假设这是一个愚蠢的问题,可以快速解决,但我没有运气 这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.L

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

        }
    }
}
提前感谢:

最后一行:

Text=txtreult.Text=

第二个=应该是a+

a=b=c=d=5;符号有效,并将所有内容设置为5。

作为以下内容的补充:

为什么要创建一个新按钮,为什么要添加该按钮的.Text,它是string.Empty,因为它还没有设置为txtreult.Text,而它已经包含一个点

优化版本为:

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