【问题标题】:How can I allow only one decimal point in a textbox?如何在文本框中只允许一个小数点?
【发布时间】:2009-03-05 08:25:44
【问题描述】:

下面是 C# 中的代码,在文本框中只允许一个小数点:

if textbox5.text.contains(".") && e.keychar="."
{
e.handled=true
}

我必须在 VB.NET 2003 版本中使用它,但我不能使用 contains 属性。我该怎么做?

【问题讨论】:

  • renu,请选择一个接受的答案。 ;)
  • Renu 不见了 :( (或:-)?)

标签: vb.net textbox


【解决方案1】:

改用Text.IndexOf(".") 方法怎么样?如果它返回 >=0,那么你已经有了一个小数点。

If (textbox5.Text.IndexOf(".") >= 0 And e.KeyChar = ".") Then e.Handled = True

【讨论】:

    【解决方案2】:

    不要忘记用户的区域设置和数字格式。我相信使用句点('.')作为十进制字符在世界范围内是少数。您可以使用CurrentUICultureNumberFormat 字段来帮助您以最正确的方式进行解析。尝试使用:

    decimal d;
    bool isValidInput = decimal.TryParse(
        textBox1.Text,
        System.Globalization.NumberStyles.AllowDecimalPoint,
        System.Globalization.CultureInfo.CurrentUICulture.NumberFormat,
        out d);
    

    这将验证数字是否输入正确。使用其他 NumberStyles 标志,您可以控制要允许的数字类型,例如千位分隔符。

    *对不起,这里是 C#。不过,VB.NET 应该很容易适应这一点;也许某个善良的灵魂可以编辑它。

    【讨论】:

      【解决方案3】:
      package Utility
      {
      
          import flash.events.Event;
      
          import flashx.textLayout.operations.PasteOperation;
      
          import spark.components.TextInput;
          import spark.events.TextOperationEvent;
      
          /**
           * This is a text input component that can only input numbers.
           * A number means a real number.
           * @author Cheng Liang
           * @version 0.0.1
           */
          public class NumberInput extends TextInput
          {
      
              [Bindable("numberChange")]
              /**
               * Sets number as input value.
               */
              public function set number(value : Number) : void
              {
                  var s : String = value.toString();
                  if(text != s) {
                      text = s;
                      dispatchEvent(new Event("numberChange"));
                  }
              }
      
              public function get number() : Number
              {
                  return Number(text);
              }
      
              public function NumberInput()
              {
                  super();
                  this.addEventListener(TextOperationEvent.CHANGE, onTextChange);
              }
      
              protected function onTextChange(event : TextOperationEvent) : void
              {
      
                  if (event.operation is PasteOperation)
                  {
                      TextInput(event.currentTarget).text='';
                  }
      
                  if(text == "" || text.length == 1) {
                      return;
                  }
      
                  /* To allow single (.) */
                  var idx : int = text.indexOf(".", 0);
                  if(idx >= 0) {
                      text = text.substring(0, idx + 1) + text.substring(idx + 1).replace(".", "");
                  }
      
                  /* To allow single (-) only at the 1st position*/
                  if(text.lastIndexOf("-")==0)
                      text ='-'+ text.substring(1);
                  else if(text.lastIndexOf("-")>0)
                  {
                      if(text.charAt(0)=='-')
                          text ='-'+ text.substring(1).replace("-", "");
                      else
                          text =text.replace("-", "");
                  }
      
                  var arr:Array = TextInput(event.currentTarget).text.split(".");
                  try{
                      if(arr[1] && String(arr[1]).length > 2){                        
                          TextInput(event.currentTarget).text = arr[0]+"."+String(arr[1]).slice(0,2);
                      }
                  }catch(err:Error){}
      
                  this.selectRange((text.length), (text.length));
              }
          }
      }
      

      【讨论】:

      • 注意在您的代码中添加一些 cmets,它在做什么,为什么,如何?
      猜你喜欢
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      相关资源
      最近更新 更多