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

本文关键字:许一个 小数点 文本 | 更新日期: 2023-09-27 17:52:42

我正在用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();
        }
    }
}

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

单击后禁用小数,如果按下操作符或'C'中的任何一个,则重新启用小数

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.

可以使用十进制。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 + ".";
}

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;
                }
            }
       }

将最后一个else更改为:

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

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

你可能应该对这个值做一些验证,以确保它是一个有效的数字

* *另一个例子,* *

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;
        }
    }

这个适合我:

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

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

private void txtweight_KeyPress(对象发送者,keypressevenargs 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;
        }

    }