1

我正在使用 C# 设计一个基本的计算器。我在输入小数点时遇到了困难。例如,如果我输入 0.8,那么它给我 0.8,如果显示屏上没有任何内容,这是正确的,但之后如果我输入十进制符号,那么它也接受 0.8 ..... 我想要一个数字只接受一个十进制符号。我的代码是

private void btn_Decimal_Click(object sender, EventArgs e)
{
    if (txt_Result.Text == "" || LastcharIssymbol==true)
    {
         txt_Result.Text = txt_Result.Text + 0 + ".";
    }
    else
         txt_Result.Text = txt_Result.Text + ".";
}

在这里,如果我输入 0.9.999 那么它也接受,如果我输入 999..... 那么它也接受。我希望一个数字 999.999 只接受一个十进制符号。请帮我。我还添加了两个额外的标签,可以显示当前系统日期和时间。我无法显示日期和时间,但我可以使用 VB.Net 显示日期和时间。我不知道我在哪里得到错误。我的整个代码是

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

namespace CS_Calculator
{
    public partial class Form1 : Form
    {
        Boolean LastcharIssymbol {get;set;}
        string op;
        double a, memory;
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_1_Click(object sender, EventArgs e)
        {
            txt_Result.Text = txt_Result.Text + "1";
            LastcharIssymbol= false;
        }

        private void btn_2_Click(object sender, EventArgs e)
        {
            txt_Result.Text = txt_Result.Text + "2";
            LastcharIssymbol = false;
        }

        private void btn_3_Click(object sender, EventArgs e)
        {
            txt_Result.Text = txt_Result.Text + "3";
            LastcharIssymbol = false;
        }

        private void btn_4_Click(object sender, EventArgs e)
        {
            txt_Result.Text = txt_Result.Text + "4";
            LastcharIssymbol = false;
        }

        private void btn_5_Click(object sender, EventArgs e)
        {
            txt_Result.Text = txt_Result.Text + "5";
            LastcharIssymbol = false;
        }

        private void btn_6_Click(object sender, EventArgs e)
        {
            txt_Result.Text = txt_Result.Text + "6";
            LastcharIssymbol = false;
        }

        private void btn_7_Click(object sender, EventArgs e)
        {
            txt_Result.Text = txt_Result.Text + "7";
            LastcharIssymbol = false;
        }

        private void btn_8_Click(object sender, EventArgs e)
        {
            txt_Result.Text = txt_Result.Text + "8";
            LastcharIssymbol = false;
        }

        private void btn_9_Click(object sender, EventArgs e)
        {
            txt_Result.Text = txt_Result.Text + "9";
            LastcharIssymbol = false;
        }

        private void btn_0_Click(object sender, EventArgs e)
        {
            txt_Result.Text = txt_Result.Text + "0";
            LastcharIssymbol = false;
        }

        private void btn_Decimal_Click(object sender, EventArgs e)
        {
            if (txt_Result.Text == "" || LastcharIssymbol==true)
            {
                txt_Result.Text = txt_Result.Text + 0 + ".";
            }
            else
                txt_Result.Text = txt_Result.Text + ".";
        }

        private void btn_Plus_Click(object sender, EventArgs e)
        {
            if(txt_Result.Text=="" || LastcharIssymbol)
            {
                MessageBox.Show("Please Enter first number to perform the addition operation.");
            }
            else
            {
                op = "+";
                txt_Result.Text = txt_Result.Text + op;
                LastcharIssymbol=true;
            }
        }

        private void btn_Minus_Click(object sender, EventArgs e)
        {
            if (txt_Result.Text == "" || LastcharIssymbol)
            {
                MessageBox.Show("Please enter first number to erform the substraction operation.");
            }
            else
            {
                op = "-";
                txt_Result.Text = txt_Result.Text + op;
                LastcharIssymbol = true;
            }
        }

        private void btn_Division_Click(object sender, EventArgs e)
        {
            if (txt_Result.Text == "" || LastcharIssymbol)
            {
                MessageBox.Show("Please enter first number to perform the division operation.");
            }
            else
            {
                op = "/";
                txt_Result.Text = txt_Result.Text + op;
                LastcharIssymbol = true;
            }
        }

        private void btn_Mult_Click(object sender, EventArgs e)
        {
            if (txt_Result.Text == "" || LastcharIssymbol)
            {
                MessageBox.Show("Please enter first number to perform the multiplication operation.");
            }
            else
            {
                op = "*";
                txt_Result.Text = txt_Result.Text + op;
                LastcharIssymbol = true;
            }
        }

        private void btn_Equal_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            txt_Result.Text = "";
        }

        private void btn_Clear_All_Click(object sender, EventArgs e)
        {
            txt_Result.Text = "";
            op = "";
            a = 0;
            memory = 0;
        }

        private void btn_Memory_Click(object sender, EventArgs e)
        {
            memory = Convert.ToDouble(txt_Result.Text);
        }

        private void btn_Show_Memory_Click(object sender, EventArgs e)
        {
            txt_Result.Text = memory.ToString();
        }
    }
}
4

8 回答 8

2

您应该在单击后禁用小数点,并在按下任何运算符或“C”时再次启用它。

于 2013-01-14T05:51:56.087 回答
1

您可以使用decimal.TryParse来测试字符串是否是有效的十进制数。如果不是,例如您的输入有两个小数点,则 TryParse 调用将失败。

TryParse 是一个不错的选择,因为除了双倍小数点之外,输入的数字可能存在许多问题,例如……三倍小数点、放错位置的减号、字母字符等。

尝试:

private void btn_Decimal_Click(object sender, EventArgs e)
{
    decimal num;
    if (!Decimal.TryParse(txt_Result.Text, out num))
    {
        MessageBox.Show(txt_Result.Text + " is not a valid number.");
        return;
    }

    if (txt_Result.Text == "" || LastcharIssymbol==true)
        txt_Result.Text = txt_Result.Text + 0 + ".";
    else
        txt_Result.Text = txt_Result.Text + ".";
}
于 2013-01-14T05:51:16.500 回答
1
if (!txt_Result.Text.Contains("."))
    if(txt_Result.Text == string.Empty)
        txt_Result.Text = "0.";
    else
        txt_Result.Text += ".";
else
    MessageBox.Show("more dots are not allowd");

如果您有像“11.9+12”这样的文本,其中有操作,您可以执行以下操作

string formula = "11.9/1.2*99.9+19";

string lastPiece = formula.Split(new char[] { '+', '-', '*', '/' })[formula.Split(new char[] { '+', '-', '*', '/' }).Count() - 1];
if (!lastPiece.Contains('.')) formula += ".";
//adds dot

lastPiece = formula.Split(new char[] { '+', '-', '*', '/' })[formula.Split(new char[] { '+', '-', '*', '/' }).Count() - 1];

if (!lastPiece.Contains('.')) formula += ".";
//does not add dot

MessageBox.Show(formula);
//output : 11.9/1.2*99.9+19.
于 2013-01-14T05:51:25.167 回答
1
private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)//textprice key pressed
    {
        if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '\b') && (e.KeyChar != '.'))
        {
            e.Handled = true;
        }
        else
        {
            e.Handled = false;
        }
        if (Char.IsControl(e.KeyChar))
        {
            e.Handled = false;
        }
        else if (Char.IsNumber(e.KeyChar) || e.KeyChar == '.')
        {
            TextBox tb = sender as TextBox;
            int cursorPosLeft = tb.SelectionStart;
            int cursorPosRight = tb.SelectionStart + tb.SelectionLength;
            string result = tb.Text.Substring(0, cursorPosLeft) + e.KeyChar + tb.Text.Substring(cursorPosRight);
            string[] parts = result.Split('.');
            if (parts.Length > 1)
            {
                if (parts[1].Length > 2 || parts.Length > 2)
                {
                    e.Handled = true;
                }
            }
       }
于 2013-02-02T14:57:44.337 回答
0

将您的最后一个更改else为:

else if (!txt_Result.Text.Contains (".")) {
    txt_Result.Text = txt_Result.Text + ".";
}

或者考虑禁用小数点按钮。

您可能应该对该值进行一些验证,以确保它是一个有效数字。

于 2013-01-14T05:51:19.167 回答
0

**另一个例子,100% **

 private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
    {

        if (txtPrice.Text.Length == 0)
        {
            if (e.KeyChar == '.')
            {
                e.Handled = true;
            }
        }
        if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 46)
        {
            e.Handled = true;
        }
        if (e.KeyChar == '.' && txtPrice.Text.IndexOf('.') > -1)
        {
            e.Handled = true;
        }
    }
于 2014-09-21T23:01:38.613 回答
0

这对我有用:

if (!txt_Result.Text.Contains("."))
if(txt_Result.Text == string.Empty)
    txt_Result.Text = "0.";
else
 MessageBox.Show("Sorry, invalid number format!    
Value can't have more than a decimal point");   
else
txt_Result.Text += "."; 
于 2021-04-23T08:16:09.383 回答
-2

将此代码放入 textbox_keypress 此处 txtweight 是您使用的我的文本框名称

私人无效txtweight_KeyPress(对象发送者,KeyPressEventArgs e){

        if (txtweight.Text.Length == 0)
        {
            if (e.KeyChar == '.')
            {
                e.Handled = true;
            }
        }
        if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 46)
        {
            e.Handled = true;
        }
        if (e.KeyChar == '.' && txtweight.Text.IndexOf('.') > -1)
        {
            e.Handled = true;
        }


    }
于 2017-03-08T12:41:46.763 回答