计算器中的小数点#

发布于 2024-08-22 22:42:51 字数 3701 浏览 5 评论 0原文

啊啊啊啊,好吧,这让我发疯。

为什么我的小数点何时出现在错误的位置,例如,

如果我在文本框中输入字符串 567,然后单击小数点按钮,我会期望(或我希望)文本框更改为 567。但相反,我得到的是 0.567

它只会进入当我添加另一个数字时的正确位置,例如如果我有数字 4,那么在执行上述操作后我会得到 567.4

编辑:

这是我的整个代码:

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;
using System.IO;

namespace Calculator
{
    public partial class frmCurrencyCalc : Form
    {
        public frmCurrencyCalc()
        {
            InitializeComponent();
        }

        private void cmdZero_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "0";
            }
            else
            {
                txtScreen.AppendText("0");
            }
        }
        private void cmd1_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "1";
            }
            else
            {
                txtScreen.AppendText("1");
            }
        }

        private void cmdTwo_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "2";
            }
            else
            {
                txtScreen.AppendText("2");
            }
        }

        private void cmdThree_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "3";
            }
            else
            {
                txtScreen.AppendText("3");
            }
        }

        private void cmdFour_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "4";
            }
            else
            {
                txtScreen.AppendText("4");
            }
        }

        private void cmdFive_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "5";
            }
            else
            {
                txtScreen.AppendText("5");
            }
        }

        private void cmdSix_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "6";
            }
            else
            {
                txtScreen.AppendText("6");
            }
        }

        private void cmdSeven_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "7";
            }
            else
            {
                txtScreen.AppendText("7");
            }
        }

        private void cmdEight_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "8";
            }
            else
            {
                txtScreen.AppendText("8");
            }
        }

        private void cmdNine_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "9";
            }
            else
            {
                txtScreen.AppendText("9");
            }
        }

       private void cmdDecimal_Click(object sender, EventArgs e)
      {
          txtScreen.AppendText(".");
          cmdDecimal.Enabled = false;
      }


        private void cmdCancel_Click(object sender, EventArgs e)
        {
            txtScreen.Text = "0";
            cmdDecimal.Enabled = true;
        }
    }
}

AHHHHH ok this is driving me nuts.

Why when does my decimal point in the wrong place e.g.

if i have the string 567 in the textbox and click the decimal button i would expect (or i want) the textbox to change to 567. but instead i get .567

It only goes into the correct place when i add another number e.g. if i had the number 4 then straight after doing the above I'd get 567.4

Edit:

Heres my whole code:

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;
using System.IO;

namespace Calculator
{
    public partial class frmCurrencyCalc : Form
    {
        public frmCurrencyCalc()
        {
            InitializeComponent();
        }

        private void cmdZero_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "0";
            }
            else
            {
                txtScreen.AppendText("0");
            }
        }
        private void cmd1_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "1";
            }
            else
            {
                txtScreen.AppendText("1");
            }
        }

        private void cmdTwo_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "2";
            }
            else
            {
                txtScreen.AppendText("2");
            }
        }

        private void cmdThree_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "3";
            }
            else
            {
                txtScreen.AppendText("3");
            }
        }

        private void cmdFour_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "4";
            }
            else
            {
                txtScreen.AppendText("4");
            }
        }

        private void cmdFive_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "5";
            }
            else
            {
                txtScreen.AppendText("5");
            }
        }

        private void cmdSix_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "6";
            }
            else
            {
                txtScreen.AppendText("6");
            }
        }

        private void cmdSeven_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "7";
            }
            else
            {
                txtScreen.AppendText("7");
            }
        }

        private void cmdEight_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "8";
            }
            else
            {
                txtScreen.AppendText("8");
            }
        }

        private void cmdNine_Click(object sender, EventArgs e)
        {
            if (txtScreen.Text == "0")
            {
                txtScreen.Text = "9";
            }
            else
            {
                txtScreen.AppendText("9");
            }
        }

       private void cmdDecimal_Click(object sender, EventArgs e)
      {
          txtScreen.AppendText(".");
          cmdDecimal.Enabled = false;
      }


        private void cmdCancel_Click(object sender, EventArgs e)
        {
            txtScreen.Text = "0";
            cmdDecimal.Enabled = true;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

灯角 2024-08-29 22:42:51

RightToLeft 看起来是你的问题。
正如 MSDN 中所述,

RightToLeft 属性用于
国际申请,其中
语言从右到书写
左边,例如希伯来语或阿拉伯语。什么时候
该属性设置为
RightToLeft..::.是,控制元素
包含文本的显示来自
从右到左。

正如之前的答案之一所建议的,这应该设置为 false,但将 TextAlign 设置为 Right 以模仿真实计算器的外观。

The RightToLeft looks to be your problem.
As described in MSDN,

The RightToLeft property is used for
international applications where the
language is written from right to
left, such as Hebrew or Arabic. When
this property is set to
RightToLeft..::.Yes, control elements
that include text are displayed from
right to left.

As one ofthe previous answers suggested, this should be set to false, but with TextAlign set to Right to mimic the appearance of a real calculator.

天冷不及心凉 2024-08-29 22:42:51

我的建议是——定义一个业务层。在你的例子中——一个双变量。单击按钮时,首先更新变量。然后格式化该值。

My advice is -- define a business layer. In your case -- a double variable. Upon button clicks, update the variable first. Then format the value.

谷夏 2024-08-29 22:42:51

我的建议是将 TextAlign 设置为 Right,但将 RightToLeft 设置为 No。

编辑:话虽如此,此问题可能与这些设置无关。

我记得 2009 年初,一位朋友在 Windows Vista 上的 Visual Studio 2008 中遇到了与此类似的错误。奇怪的是,在 Windows XP 上的同一版本的 Visual Studio 上并没有出现同样的问题。

如果您尚未将 Visual Studio / .NET 3.5 更新为 Service Pack 1,我建议这样做并查看是否可以解决问题。

My advice is to set TextAlign to Right, but leave RightToLeft set to No.

Edit: Having said that, this issue may be unrelated to these settings.

I remember a friend having this a bug similar to this back in early 2009 in Visual Studio 2008 on Windows Vista. Strangely enough, the same problem did not occur on the same version of Visual Studio on Windows XP.

If you haven't updated Visual Studio / .NET 3.5 to Service Pack 1, I suggest doing that and seeing if it fixes the problem.

笙痞 2024-08-29 22:42:51

也许尝试不同的方法:(

private void AddDecimal()
{
    txtScreen.SelectionLength = txtScreen.TextLength;
    txtScreen.SelectedText += ".";
}

还有你的文本框、文本对齐方式、右对齐...如果不是,可能会导致你的问题。)

Perhaps try a different method:

private void AddDecimal()
{
    txtScreen.SelectionLength = txtScreen.TextLength;
    txtScreen.SelectedText += ".";
}

(Also is your text box, text aligment, right aligned... if not that may contribute to your problem.)

雄赳赳气昂昂 2024-08-29 22:42:51

我想你这里有一些东西。

从表面上看,您已经设置了:

txtScreen。从右到左 = true;

如果您仅附加小数点,您将得到您所描述的结果。尝试使用类似的东西:

txtScreen.AppendText(".00");

这将为您提供您所描述的结果。

你可能会打开一罐蠕虫。当您开始格式化文本框时,您正在将其从保存值更改为表示形式。例如:

decimal value = 567;
txtScreen.Text = value.ToString("0.00");

那么你将不得不开始编写疯狂的验证规则以避免像 567.00.1 等值。

I think you have a few things here.

By the looks of it, you've set:

txtScreen.right to left = true;

If you append just the decimal point, you get the result you describe. Try using something like:

txtScreen.AppendText(".00");

This will give you the result you are describing.

You could be opening a can of worms. When you start formatting the textbox, you are changing it from holding a value to presentation. Eg:

decimal value = 567;
txtScreen.Text = value.ToString("0.00");

Then you will have to start writing crazy validation rules to avoid values like 567.00.1 etc.

流年里的时光 2024-08-29 22:42:51

只是为了让大家知道谁有兴趣。

我设法以某种方式修复它。我所做的就是删除设计代码中从右到左的内容,然后将其重新对齐(使用 GUI)向右,它的工作...奇怪,因为我与上次没有做任何不同的事情...

哦,好吧,

谢谢大家帮助

非常感谢

x

Just to let you all know who are interested.

I managed to fix it somehow. All I did was delete the right to left thing in the design code and then realigned it (using the GUI) to right and its worked...odd as I did nothing different to last time...

Oh well

Thank you all for your help

Much aprreciated

x

勿忘初心 2024-08-29 22:42:51

你可以试试这个,它对我有用:

private void btndot_Click(object sender, EventArgs e)
{
    if (txtbox.Text == "0" && txtbox.Text != null)
    {
        txtbox.Text = ".";
    }
    else
    {
        txtbox.Text = txtbox.Text + ".";
    }
}

You can try this one, it works for me:

private void btndot_Click(object sender, EventArgs e)
{
    if (txtbox.Text == "0" && txtbox.Text != null)
    {
        txtbox.Text = ".";
    }
    else
    {
        txtbox.Text = txtbox.Text + ".";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文