繁体   English   中英

如何在文本框中只允许一个小数点?

[英]How can I allow only one decimal point in a textbox?

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

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

我必须在 VB.NET 2003 版本中使用它,但我不能使用 contains 属性。 我怎样才能做到这一点?

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

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

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

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

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

*在这里对 C# 感到抱歉。 尽管如此,VB.NET 应该很容易适应; 也许某个善良的灵魂可以编辑它。

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));
        }
    }
}

暂无
暂无

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

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