简体   繁体   English

在VB.NET中检查屏蔽文本框上的小数

[英]Check for decimal on masked textbox in VB.NET

I've got a masked textbox with the mask to be _____.__ . 我有一个蒙版文本框,其蒙版为_____.__

Now someone can just put in 00001.__ . 现在有人可以只输入00001.__ And it still works, but I need people to enter a decimal number. 它仍然有效,但是我需要人们输入一个十进制数字。 Also, how can I put a check on this? 另外,我该如何检查呢?

In an event like TextChanged you can use: 在类似TextChanged的事件中,您可以使用:

if (maskedTextBox1.MaskCompleted == true)
{
    // Invalid input
}

I believe it should be better just to convert the textbox to a decimal or double, since the lack of decimal numbers just means ".00". 我认为将文本框转换为十进制或双精度会更好,因为缺少十进制数字仅表示“ .00”。

So, you may: 因此,您可以:

  1. Set the mask of control to "000000.00" or even use as it is now. 将控制蒙版设置为“ 000000.00”,甚至立即使用。
  2. Convert it to double or decimal: Dim DecNum as Double = CDbl(maskedTextBox.text) Dim DecNum as Decimal = CDec(maskedTextBox.text) 将其转换为双精度或十进制:Dim DecNum为Double = CDbl(maskedTextBox.text)Dim DecNum为十进制= CDec(maskedTextBox.text)

So, if the user had informed a decimal number, you would have it; 因此,如果用户通知了十进制数字,则可以使用; otherwise your number will consider ".__" as ".00". 否则,您的电话号码会将“ .__”视为“ .00”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM