Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文本框中只允许一个小数点_C#_String_Parsing_Textbox - Fatal编程技术网

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

C# 文本框中只允许一个小数点,c#,string,parsing,textbox,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) {

我正在用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();
        }
    }
}
您可以使用测试字符串是否为有效的十进制数。如果没有,例如如果您的输入有两个小数点,那么TryParse调用将失败

TryParse是一个很好的选择,因为除了双小数点之外,输入的数字可能会有很多问题,例如。。。三个小数点,一个错位的减号,阿尔法字符等

尝试:


将您最后的
else
更改为:

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

或考虑禁用小数点按钮。

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

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.

单击小数点后应禁用它,如果按下任何运算符或“C”,应再次启用它。

private void txtPrice\u KeyPress(对象发送者,KeyPressEventArgs e)//textprice键按下
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;
                }
            }
       }
{ 如果((e.KeyChar<'0'| e.KeyChar>'9')&&(e.KeyChar!='\b')&&(e.KeyChar!='')) { e、 已处理=正确; } 其他的 { e、 已处理=错误; } if(Char.IsControl(e.KeyChar)) { e、 已处理=错误; } else if(Char.IsNumber(e.KeyChar)| | e.KeyChar=='。) { TextBox tb=发送方作为TextBox; int cursorPosLeft=tb.SelectionStart; int cursorPosRight=tb.SelectionStart+tb.SelectionLength; 字符串结果=tb.Text.Substring(0,cursorPosLeft)+e.KeyChar+tb.Text.Substring(cursorPosRight); string[]parts=result.Split('.'); 如果(零件长度>1) { if(零件[1]。长度>2 | |零件。长度>2) { e、 已处理=正确; } } }
**另一个例子,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;
        }
    }

将此代码放入文本框中 这里txtweet是我的文本框名称,您可以使用您的文本框名称

私有void txtwweight\u按键(对象发送器,按键事件参数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;
        }


    }
这对我很有用:

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

请一次问一个问题。您的标签和日期/时间代码在哪里?请将代码向后修剪。大部分都是不必要的。请只显示给您带来问题的内容。还有,这是家庭作业吗?谢谢Paul。我已经完成了显示日期和时间的日历。我已经编写了lbl_time_Disp.Text=DateTime.Now.ToLongTimeString();lbl_Date_Disp.Text=DateTime.Now.ToLongDateString();在Form_Load event和Timer_Tick event下,我编写了lbl_Time_Disp.Text=DateTime.Now.ToLongTimeString();我认为这个代码是正确的。一个建议@Pritam,尽量不要把你的问题弄得一团糟,否则人们不会把它读到最后,因为它就像一本2000页的书:)谢谢Mehdi,我以后会处理这些事情。@Pritam,不客气,我的朋友。嗨,Mahdi,这里,如果我正在执行像11.9+99.9这样的操作,那么第二个数字那是99.9,它不接受十进制符号。请帮帮我。我是C#的新手。@Pritam it'ok,朋友问你问题。好了,99.9已经有了一个
,为什么它要接受另一个呢?哦,我想我刚才明白你的意思了,你的意思是你的文本框也包含操作,对吗?是的,我的文本框也包含操作。我希望答案是by Sumit可能会有帮助。@我希望它也有帮助,我编辑的答案也很好。祝你好运。
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 += ".";