none
How can Delete numbers from text-boxes by using Backspace button? RRS feed

  • Question

  • I have many text-boxes in my forms. I have applied the code for allowance just numbers and (.) in my text-text-boxes. When I want to clear any number from any text-boxes by using backspace button the VB program show error end close the debugging.

    so how can I prevent from this error and can delete numbers?

    thanks in advance

    Actually this is the thing that i want




    • Edited by Amranullah Tuesday, October 29, 2019 1:17 PM
    Tuesday, October 29, 2019 1:16 PM

All replies

  • Hi

    Try this: (include Chr(8) as an 'allowed' input)

    Dim allowed As String = "0123456789.+-" & Chr(8)


    Regards Les, Livingston, Scotland

    Tuesday, October 29, 2019 1:20 PM
  • Hello,

    To accept numbers with decimal separator use

    Private Sub NumbersOnlyTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles NumbersOnlyTextBox.KeyPress
        Dim decimalSeparator As String = Application.CurrentCulture.NumberFormat.NumberDecimalSeparator
        e.Handled = Not (Char.IsDigit(e.KeyChar) Or
            Asc(e.KeyChar) = 8 Or
               (e.KeyChar = decimalSeparator And
                CType(sender, TextBox).Text.IndexOf(decimalSeparator, StringComparison.Ordinal) = -1))
    End Sub

    When ready to use the text as decimal for instance use

    Dim value = 0D
    If Decimal.TryParse(NumbersOnlyTextBox.Text, value) Then
        ' use it
    Else
        ' do not use it
    End If


    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

    Tuesday, October 29, 2019 1:27 PM
    Moderator
  • not worked

    Amranullah

    Tuesday, October 29, 2019 2:46 PM
  • not worked

    Amranullah

    Hi

    You are so helpful. Please, can you not shorten your information to stop us becoming tired from reading it all?


    Regards Les, Livingston, Scotland

    Tuesday, October 29, 2019 3:04 PM
  • Hi,

    Please provide your code so we can assist you, without code it is purely guess work. When providing the code please indicate the methods and events also.

    Best Regards,

    Julie


    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.

    Wednesday, October 30, 2019 3:23 AM
    Moderator