简体   繁体   English

如果文本框具有字符串,则显示msgbox,但(。)不计数。 视觉基础

[英]if textbox has string then show msgbox but the (.) doesnt count. visual basic

i have this textbox where i want to input only number the (.) included. 我在此文本框中只想输入包括的数字(。)。 for example is 190.5. 例如190.5。

but if it has text for example 190.5g then it will show msgbox("error") 但是,如果它有文本,例如190.5g,它将显示msgbox(“ error”)

i have this code i found somewhere 我有在某个地方找到的这段代码

 Dim allDigit = pbox.Text.Trim.Length <> 0 AndAlso _
      pbox.Text.All(Function(chr) Char.IsDigit(chr))
        If Not allDigit Then
            MsgBox("Please input number only on price")
            pbox.Clear()
            Exit Sub
        End If

if i add . 如果我加上。 on the number it shows the msgbox so is there anyway to include the . 在其上显示msgbox的数字上,因此无论如何都包含。 ?

Check out Decimal.TryParse , rather than pulling the string apart yourself. 请检查Decimal.TryParse ,而不是自己将字符串拉开。

Dim value As Decimal
Dim yourString As String = "1234"
If Not Decimal.TryParse(yourString, value) Then
    MsgBox("Please input number only on price")
    pbox.Clear()
    Exit Sub
End If

It should be noted that the character that represents the decimal separator will vary depending on the language settings of the OS - for American / British English it'll be period, for German it'll be comma. 应该注意的是,代表小数点分隔符的字符将根据操作系统的语言设置而有所不同-对于美国/英国英语将是句点,对于德语将是逗号。

Use IsNumeric function instead 改用IsNumeric函数

    If Not IsNumeric(pbox.Text) Then
        MsgBox("Please input number only on price")
        pbox.Clear()
        Exit Sub
    End If

This is my code in c# which i use 这是我在C#中使用的代码

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar) 
            && !char.IsDigit(e.KeyChar) 
            && e.KeyChar != '.')
        {
                            //message box
            e.Handled = true;
        }

        // only allow one decimal point
        if (e.KeyChar == '.' 
            && (sender as TextBox).Text.IndexOf('.') > -1)
        {
                            //message box
            e.Handled = true;
        }
    }

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

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