C# 计算器c中的小数点#
啊,好吧,这让我快发疯了 为什么我的小数点在错误的地方 如果文本框中有字符串567,并单击十进制按钮,我希望(或希望)文本框更改为567。但是我却得到了 只有当我添加另一个数字时,它才会进入正确的位置,例如,如果我有数字4,那么在完成上述操作后,我会得到567.4 编辑: 这是我的全部密码:C# 计算器c中的小数点#,c#,decimal,calculator,C#,Decimal,Calculator,啊,好吧,这让我快发疯了 为什么我的小数点在错误的地方 如果文本框中有字符串567,并单击十进制按钮,我希望(或希望)文本框更改为567。但是我却得到了 只有当我添加另一个数字时,它才会进入正确的位置,例如,如果我有数字4,那么在完成上述操作后,我会得到567.4 编辑: 这是我的全部密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using S
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;
}
}
}
或许可以尝试另一种方法:
private void AddDecimal()
{
txtScreen.SelectionLength = txtScreen.TextLength;
txtScreen.SelectedText += ".";
}
(还有你的文本框,文本对齐,右对齐……如果不是的话,这可能会导致你的问题。)我想你这里有一些东西 从外观上看,您已设置: txtScreen.=是的 如果只添加小数点,则得到所描述的结果。尝试使用以下方法: txtScreen.AppendText(“.00”) 这将为您提供所描述的结果 你可能正在打开一罐虫子。当您开始格式化文本框时,您正在将其从保存值更改为显示。例如:
decimal value = 567;
txtScreen.Text = value.ToString("0.00");
然后,您必须开始编写疯狂的验证规则,以避免567.00.1等值。我的建议是将
TextAlign
设置为右侧,但将Right设置为左侧
编辑:尽管如此,这个问题可能与这些设置无关
我记得一个朋友在2009年初在WindowsVista上的VisualStudio2008中遇到了类似的bug。奇怪的是,在Windows XP上的同一版本的Visual Studio上没有出现相同的问题
如果您尚未将Visual Studio/.NET 3.5更新为,我建议您这样做,看看它是否解决了问题。右侧到左侧的似乎是您的问题。
如MSDN中所述
RightToLeft属性用于
国际应用程序
语言是从右到右书写的
左,如希伯来语或阿拉伯语。什么时候
此属性设置为
右至左...:.是,控制元素
其中包含从中显示的文本
从右到左
正如前面的一个答案所建议的,这应该设置为false,但是将TextAlign
设置为Right,以模拟真实计算器的外观。我的建议是——定义一个业务层。在你的例子中——一个双变量。单击按钮后,首先更新变量。然后格式化值。只是为了让大家知道谁感兴趣
我设法把它修好了。我所做的只是删除设计代码中从右到左的部分,然后(使用GUI)将其重新调整到右,这样做很奇怪,因为我上次没有做什么不同的事情
哦,好吧
谢谢大家的帮助
非常感谢
x我们不知道txtScreen是什么。它是一个文本框。想象一个计算器。txtScreen是显示屏幕这段代码只是我全部代码的一部分我写了一个小样本应用程序来测试这一点,但似乎无法重现您所描述的内容。还有什么东西不见了吗?您正在对代码中插入点的位置进行处理吗?我的文本框从右向左对齐是否会改变它。@Pops-如果我的建议不起作用,那么代码中的其他地方一定会有不必要的副作用。txtScreen上有任何事件处理程序吗?例如,一个向下键处理程序或其他移动插入点的处理程序?我将禁用十进制按钮,这样就不会发生这种情况。是的,它是从右到左。我将它设置为通过gui界面(在属性中)对齐,而不是在代码中对齐。这有区别吗?不,它会有同样的效果。Windows窗体设计器在[FormName].Designer.cs file.Fyi中生成与您键入的代码相同的代码。我使用VS 2008运行Windows 7,并通过将RightToLeft设置为True复制了您的问题。至少对我来说,关闭RightToLeft可以解决这个问题。为什么不使用decimal
变量呢?没有浮点舍入。