在文本框中只允许一个小数点

时间:2013-01-14 05:47:55

标签: c# string parsing textbox

我正在使用C#设计基本计算器。我输入小数点时遇到困难。例如,如果我输入.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();
        }
    }
}

8 个答案:

答案 0 :(得分:2)

单击后应禁用小数,如果按下任何操作符或“C”,则再次启用小数。

答案 1 :(得分:1)

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

TryParse是一个不错的选择,因为输入的数字可能存在许多问题,除了加倍的小数点,例如......三位小数点,错位的减号,alpha字符等。

尝试:

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 + ".";
}

答案 2 :(得分: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.

答案 3 :(得分: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;
                }
            }
       }

答案 4 :(得分:0)

将您的上一个else更改为:

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

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

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

答案 5 :(得分: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;
        }
    }

答案 6 :(得分:0)

这对我有用:

/home

答案 7 :(得分:-2)

将此代码放在textbox_keypress中 这里txtweight是我用你的文本框名称

private void txtweight_KeyPress(object sender,KeyPressEventArgs e)         {

<body ng-app="scotchApp">