Results 1 to 4 of 4

Thread: [RESOLVED] TextBox Question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    92

    Resolved [RESOLVED] TextBox Question

    My question is I have a masked text box for a phone number to be entered. I want the user to be only able to enter numbers and the backspace key. I don't want the user to be able to use the space button. Here is the code I have:
    Code:
    Private Sub xPhoneNumberTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
          Handles xPhoneNumberTextBox.KeyPress
    
          If e.KeyChar = " "c Then
             e.Handled = True
          ElseIf e.KeyChar < "0" OrElse e.KeyChar > "9" AndAlso e.KeyChar <> ControlChars.Back Then
             e.Handled = True
          End If
       End Sub
    My problem is spaces can still be entered into the text box. What am I doing wrong?

  2. #2
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    UK
    Posts
    489

    Re: TextBox Question

    Hi

    Try this:

    Code:
            Dim InputtedKey As Boolean = Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar)
            If InputtedKey = False Then
                e.Handled = True
            End If
    Learning C♯

    Data Binding & Bound Controls - Objects and wizards will never be as intelligent as you, do it yourself! (Unless your Pro)

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    92

    Re: TextBox Question

    thanks. I got it to work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width