只允许一个在文本框中小数点 [英] Allow only one decimal point in a Text Box

查看:104
本文介绍了只允许一个在文本框中小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计使用C#一个基本的计算器。我现在面临的困难,当我进入了一个小数点。例如,如果我进入0.8,然后它给了我0.8是正确的,如果没有什么在显示屏上,但之后如果我进入小数点符号,那么它也接受它是0.8 .....我想只有一个小数点符号为一个号码被接受。我的代码是



 私人无效btn_Decimal_Click(对象发件人,EventArgs五)
{
如果(txt_Result。文本==|| LastcharIssymbol == true)而
{
txt_Result.Text = txt_Result.Text + 0 +。;
}
,否则
txt_Result.Text = txt_Result.Text +。;
}



在这里,如果我进入0.9.999随后也接受它,如果我我进入999 .....那么它也接受。我想只有一个小数点符号为一个数字,它是999.999被接受。请帮帮我。我也是增加两个额外的标签,可以显示当前系统日期和时间。我无法显示日期和时间,但我能证明使用VB.Net的日期和时间。我不知道我在哪里得到的错误。我的整个代码是



 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;

命名空间CS_Calculator
{
公共部分Form1类:表格
{
布尔LastcharIssymbol {获取;集;}
字符串运算;
翻一番,记忆;
公共Form1中()
{
的InitializeComponent();
}

私人无效btn_1_Click(对象发件人,EventArgs五)
{
txt_Result.Text = txt_Result.Text +1;
LastcharIssymbol = FALSE;
}

私人无效btn_2_Click(对象发件人,EventArgs五)
{
txt_Result.Text = txt_Result.Text +2;
LastcharIssymbol = FALSE;
}

私人无效btn_3_Click(对象发件人,EventArgs五)
{
txt_Result.Text = txt_Result.Text +3;
LastcharIssymbol = FALSE;
}

私人无效btn_4_Click(对象发件人,EventArgs五)
{
txt_Result.Text = txt_Result.Text +4;
LastcharIssymbol = FALSE;
}

私人无效btn_5_Click(对象发件人,EventArgs五)
{
txt_Result.Text = txt_Result.Text +5;
LastcharIssymbol = FALSE;
}

私人无效btn_6_Click(对象发件人,EventArgs五)
{
txt_Result.Text = txt_Result.Text +6;
LastcharIssymbol = FALSE;
}

私人无效btn_7_Click(对象发件人,EventArgs五)
{
txt_Result.Text = txt_Result.Text +7;
LastcharIssymbol = FALSE;
}

私人无效btn_8_Click(对象发件人,EventArgs五)
{
txt_Result.Text = txt_Result.Text +8;
LastcharIssymbol = FALSE;
}

私人无效btn_9_Click(对象发件人,EventArgs五)
{
txt_Result.Text = txt_Result.Text +9;
LastcharIssymbol = FALSE;
}

私人无效btn_0_Click(对象发件人,EventArgs五)
{
txt_Result.Text = txt_Result.Text +0;
LastcharIssymbol = FALSE;
}

私人无效btn_Decimal_Click(对象发件人,EventArgs五)
{
如果(txt_Result.Text ==|| LastcharIssymbol == true)而
{
txt_Result.Text = txt_Result.Text + 0 +。
}
,否则
txt_Result.Text = txt_Result.Text +。;
}

私人无效btn_Plus_Click(对象发件人,EventArgs五)
{
如果(txt_Result.Text ==|| LastcharIssymbol)
{
MessageBox.Show(请输入第一个数字来执行加法运算。);
}
,否则
{
OP =+;
txt_Result.Text = txt_Result.Text +运算;
LastcharIssymbol = TRUE;
}
}

私人无效btn_Minus_Click(对象发件人,EventArgs五)
{
如果(txt_Result.Text ==|| LastcharIssymbol)
{
MessageBox.Show(请输入第一个数字为erform的减法操作。);
}
,否则
{
OP = - ;
txt_Result.Text = txt_Result.Text +运算;
LastcharIssymbol = TRUE;
}
}

私人无效btn_Division_Click(对象发件人,EventArgs五)
{
如果(txt_Result.Text ==|| LastcharIssymbol)
{
MessageBox.Show(请输入第一个数字执行除法运算。);
}
,否则
{
OP =/;
txt_Result.Text = txt_Result.Text +运算;
LastcharIssymbol = TRUE;
}
}

私人无效btn_Mult_Click(对象发件人,EventArgs五)
{
如果(txt_Result.Text ==|| LastcharIssymbol)
{
MessageBox.Show(请输入第一个数字进行乘法运算。);
}
,否则
{
运=*;
txt_Result.Text = txt_Result.Text +运算;
LastcharIssymbol = TRUE;
}
}

私人无效btn_Equal_Click(对象发件人,EventArgs五)
{

}

私人无效button4_Click(对象发件人,EventArgs五)
{
txt_Result.Text =;
}

私人无效btn_Clear_All_Click(对象发件人,EventArgs五)
{
txt_Result.Text =;
运=;
A = 0;
内存= 0;
}

私人无效btn_Memory_Click(对象发件人,EventArgs五)
{
内存= Convert.ToDouble(txt_Result.Text);
}

私人无效btn_Show_Memory_Click(对象发件人,EventArgs五)
{
txt_Result.Text = memory.ToString();
}
}
}


解决方案

 如果(!txt_Result.Text.Contains(。))
如果(txt_Result.Text ==的String.Empty)
txt_Result.Text = 0;
,否则
txt_Result.Text + =。
,否则
MessageBox.Show(多点并不被allowd);



你有这样的文字像'11 0.9 +这有业务12'你可以做以下

 串公式=11.9 / 1.2 * 99.9 + 19; 

串lastPiece = formula.Split(新的char [] {'+',' - ','*','/'})[formula.Split(新的char [] {'+' 。,' - ','*','/'})计数() - 1]; (!'。'lastPiece.Contains())
如果公式+ =。
//添加点

lastPiece = formula.Split(新的char [] {'+',' - ','*','/'})[formula.Split(新。的char [] {'+',' - ','*','/'})计数() - 1]; (!'。'lastPiece.Contains())

如果公式+ =。
//不加点

MessageBox.Show(公式);
//输出:11.9 / 1.2 * 99.9 + 19。


I am designing a basic calculator using C#. I am facing difficulties while I am entering a decimal point. Example, if I am entering .8 then it gives me 0.8 which is correct if there is nothing on the display screen but after that if I am entering the decimal symbol, then also it accepts it that is 0.8..... I want only one decimal symbol to be accepted for one number. My code is

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

Here, if I am entering 0.9.999 then also it accepts and if I am entering 999..... then also it accepts. I want only one decimal symbol to be accepted for one number that is 999.999. Please help me. Also I am adding two additional labels that can show the current system date and time. I am unable to show the date as well as the time but I am able to show the date and time using VB.Net. I don't know where I am getting the errors. My whole code is

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

解决方案

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");

for the case you have text like '11.9+12' which has operations in it you can do the following

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.

这篇关于只允许一个在文本框中小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆