【问题标题】:I want that the user can only type x.x.x.x in a Textbox我希望用户只能在文本框中键入 x.x.x.x
【发布时间】:2014-08-01 11:31:57
【问题描述】:

我希望用户只能在文本框中输入 X.X.X.X(X 是一个数字)

我尝试了一个 maskedTextbox 并在属性下将掩码更改为:

//Mask: 9.9.9.9

//Previw: _,_,_,_

但是当我从 maskedTextbox 中得到值时,我得到:

//Value: 1,1,1,1

有没有可能得到:

//Value : 1.1.1.1

【问题讨论】:

  • 您的文化设置是什么?尝试将NumberDecimalSeparator 设置为点.
  • 您需要一个只接受数字和点的文本框,不是吗?
  • 我的文化设置:de - @Sujith Karivelil:是的,但必须按以下顺序:“x.x.x.x”
  • 我猜你的文化是 nl-NL 因为它对待 .作为,
  • 属性中没有是culture = de - At

标签: winforms textbox maskedtextbox


【解决方案1】:

这在documentation for the Mask property:

. 小数占位符。实际使用的显示字符将是适合格式提供程序的十进制符号,由控件的 FormatProvider 属性确定。

...

\ 转义。转义掩码字符,将其转换为文字。 "\\" 是反斜杠的转义序列。

因此,如果您总是想要一个不考虑区域设置的句号,则不应使用.:在这种情况下,您应该使用\.

【讨论】:

  • 谢谢,我将其设置为“正确答案”,但我现在不能这样做(抱歉,我不能将此答案设置为有用,因为我没有 15 名声望),有美好的一天:)
【解决方案2】:

我猜你的文化创造问题是 de-At 将 . 视为 , 所以你需要将其转换为“。”

试试下面的代码:-

System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";

System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2020-09-18
    • 2017-08-10
    相关资源
    最近更新 更多