locked
May i ask how to make textbox only allow (-,.(dot)and number) RRS feed

  • Question

  • I write code about textbox allow numbers (-) and (.) dot
    However, the (-) aspect is unavailable. can help me take a look. Thanks.
    Below is my code

    If sg1.Text = "" Then
                'sg1.Focus()
                'sg1.SelectAll()
            Else
                Try
                    S1 = CDbl(CStr(sg1.Text))
                Catch ex As Exception
                    'MsgBox("Pls fill correct number")
                    sg1.Text = ""
                End Try

            End If

     If Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = "." Or e.KeyChar = "-") And Not Char.IsControl(e.KeyChar) Then
                e.Handled = True
            End If


    • Edited by christing Thursday, July 9, 2020 8:26 AM
    Thursday, July 9, 2020 8:23 AM

All replies

  • use this

    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not Char.IsControl(e.KeyChar) AndAlso (
               Not Char.IsDigit(e.KeyChar)) AndAlso (e.KeyChar <> "."c) AndAlso (e.KeyChar <> "-"c) Then
            e.Handled = True
        End If
    
        ' only allow one decimal point
        If e.KeyChar = "."c AndAlso CType(sender, TextBox).Text.IndexOf("."c) > -1 Then
            e.Handled = True
        End If
    
        ' only allow minus sign at the beginning
        If e.KeyChar = "-"c AndAlso CType(sender, TextBox).Text.Length > 0 Then
            e.Handled = True
        End If
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, July 10, 2020 12:53 AM
  • Hi christing,
    Here is a simple code example you can also refer to.

    Private Sub textBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = "." Or e.KeyChar = "-" )
    End Sub

    Best Regards,
    Daniel Zhang


    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.

    Friday, July 10, 2020 3:27 AM
  • Hi christing,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    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.

    Monday, July 13, 2020 5:09 AM