Allow only one decimal point in a Text Box

2019-05-31 00:21发布

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

7条回答
劳资没心,怎么记你
2楼-- · 2019-05-31 01:06
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;
                }
            }
       }
查看更多
登录 后发表回答