我现在有一个9.99的掩码,#是代表'+‘或'-’符号的。
当我设置蒙面文本框的值(其中值为负值)时,它将给出正确的值。
然而,当值为正时,它给出了不正确的答案。
下面是测试它的实际代码。例如:
private void Form1_Load(object sender, EventArgs e){
double value = -0.14;
double value2 = 0.14;
maskedTextBox1.Text = value.ToString();
maskedTextBox2.Text = value2.ToString();
}

我需要它是_0.14
设置来自RightToLeft的属性不起作用,因为我不希望用户从右到左键入。
任何帮助都是非常感谢的。
发布于 2013-07-17 11:39:16
我想你需要垫一下:
maskedTextBox2.Text = value2.ToString().Length < maskedTextBox2.Mask.Length ?
value2.ToString().PadLeft(maskedTextBox2.Mask.Length,' '): value2.ToString();对任择议定书的评论作出答复:
// Pad the left of decimal with spaces
string leftOfDecimal = value.ToString().Split('.')[0].PadLeft(maskedTextBox1.Mask.Split('.')[0].Length);
// Pad the right with 0s
string rightOfDecimal = value.ToString().Split('.')[1].PadRight(maskedTextBox1.Mask.Split('.')[1].Length,'0');
maskedTextBox1.Text = leftOfDecimal + "." + rightOfDecimal;请注意,您必须检查value是否有小数点。上面的代码没有这样的检查。如果输入double value = 25,它就会爆炸。可能还有其他的边缘情况,您将不得不处理。
发布于 2013-07-17 15:56:27
直接设置Text不起作用,因为Mask将决定文本的显示方式。0.14将首先成为014 (因为点将被忽略),然后014将在从Mask应用格式后成为01.4_。如果您想直接设置Text,您可能必须创建您自己的MaskedTextBox,尽管这很容易,但我想给出我们使用extension method的解决方案。
public static class MaskedTextBoxExtension {
public static void SetValue(this MaskedTextBox t, double value){
t.Text = value.ToString(value >= 0 ? " 0.00" : "-0.00");
}
}
//I recommend setting this property of your MaskedTextBox to true
yourMaskedTextBox.HidePromptOnLeave = true;//this will hide the prompt (which looks ugly to me) if the masked TextBox is not focused.
//use the code
yourMaskedTextBox.SetValue(0.14);// => _0.14
yourMaskedTextBox.SetValue(-0.14);// => -0.14https://stackoverflow.com/questions/17697544
复制相似问题