none
Maskedtextbox only accept a value with two decimals (VB Windows form) RRS feed

  • Question

  • Hello,

    I'm building an calculator in Visual Studio (Visual Basic) and I'm stuck with the Maskedtextbox control. I want that the user only can give a value with two decimals. A value of 2.20 needs to be accepted and a value of 99.93. I want a code that only accept a value between 1 and 100 with two decimals.

    If value.MaskCompleted = true Then

    Value * 2

    Else

    MsgBox("The calculator only accept a value with two decimals!", , "Input"

    End if

    I now have my mask set to ###.00, but my output is shown with two blank lines when I have a input of 9,93.

    Is my idea possible with the Maskedtextbox control?

    Thanks in advance

    Jonathan 


    • Edited by Jonathan.12 Sunday, November 10, 2019 3:18 PM
    Sunday, November 10, 2019 2:01 PM

Answers

  • Hi Jonathan.12,

    Welcome to MSDN forum.

    The maskset is used to limit the format entered by the user. For example "###.00", it means that the input must be 3 integers and 2 decimals and if just input 1 or 2 integers, you need to press space. So to judge 2 integers, we need to delete the spaces within "value".

    Private Sub MaskedTextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MaskedTextBox1.Validating
    
    Dim value As String = Me.MaskedTextBox1.Text
    
    value = Replace(value, " ", "") 'delete space'
    
    If Convert.ToDouble(value) >= 100 Then
       MsgBox("The calculator only accept a value with two decimals!", , "Input")
    Else
       MsgBox("ok", , "Input")
    End If
    
    End Sub

    Hope it could help you.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    • Marked as answer by Jonathan.12 Tuesday, November 12, 2019 6:25 PM
    Monday, November 11, 2019 10:22 AM

All replies

  • Hi Jonathan.12,

    Welcome to MSDN forum.

    The maskset is used to limit the format entered by the user. For example "###.00", it means that the input must be 3 integers and 2 decimals and if just input 1 or 2 integers, you need to press space. So to judge 2 integers, we need to delete the spaces within "value".

    Private Sub MaskedTextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MaskedTextBox1.Validating
    
    Dim value As String = Me.MaskedTextBox1.Text
    
    value = Replace(value, " ", "") 'delete space'
    
    If Convert.ToDouble(value) >= 100 Then
       MsgBox("The calculator only accept a value with two decimals!", , "Input")
    Else
       MsgBox("ok", , "Input")
    End If
    
    End Sub

    Hope it could help you.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    • Marked as answer by Jonathan.12 Tuesday, November 12, 2019 6:25 PM
    Monday, November 11, 2019 10:22 AM
  • Thank you, it is working.
    Tuesday, November 12, 2019 6:25 PM