Click here to Skip to main content
15,914,066 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey, everybody. I have a piece of VB code. What I want to do is to let the TextBox to accpet certain charaters. However, after I use the following code, the hot key like "Ctrl + V", "Ctrl + C", etc are no longer working. How can I modify the following code to let the Textbox support the hot keys from the Keyboard?

VB
Private Sub TextBox1_KeyPress(ByVal sender As   Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles   TextBox1.KeyPress
    Try
        If Char.IsDigit(e.KeyChar) Or e.KeyChar = ". "   Or e.KeyChar = Chr(8) Then
            If e.KeyChar = ". " And InStr(Me.Text, ". ") > 0 Then
                   e.Handled = True
            Else
                   e.Handled = False
            End If
         Else
            e.Handled = True
         End If
     Catch ex As Exception
     End Try
End Sub

Thank you very much in advance

[Modifed: use pre tags!!!! and take the little extra time to properly format the code (as in remove extra spaces and fix tabs). You'll get more responses that way and they'll tend to be more helpful]
Posted
Updated 24-Jun-10 6:34am
v2

cnatsa71 wrote:
Else e.Handled = True End If

Not sure - but try by removing the e.Handled code above...
 
Share this answer
 
Since you're doing things by 'KeyPress' you'll have to check the Ctrl+V
event:
Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

        If (e.Control) Then
            If (e.KeyCode = Keys.V) Then
                Text1 = Clipboard.GetText
                TextBox1.SelectedText = Text1
            End If
        End If

    End Sub

Check if Text1 are all numbers. If so, put them in TextBox1.
 
Share this answer
 
Abhinav said what your problem was, but I don't understand what you're thought you were doing with that code.

First, absolutely nothing in that code could throw an exception, unless Me.Text doesn't exist. That means that you do not need a Try/Catch block there. Secondly, don't put everything in a Try/Catch block. If you want the old method, .NET still has the On Error Goto junk. Try/Catch is meant to allow you to handle specific statements that could throw an error.

Now, what exactly does your code do? (because it doesn't make a lot of sense)

Here's how your If statements currently work...

Is e.KeyChar a digit? Or, is e.KeyChar equal to the string ". "? (which it never will be because a single char can't be equal to two characters) Or is e.KeyChar equal to the Char representation of 8? (which you already checked to see if it was a digit so why would you check if it was a specific digit?) (Actually, I just realized that you were checking for backspace)

    If any one of those is true, then check if e.KeyChar is equal to ". "
    (which again it will never be) or does Me.Text contain ". "?

       If any of those are true, don't allow a keypress


The only time you actually allow a KeyPress is if the key pressed was a digit and Me.Text didn't have ". " in it.

The other answer showed you how to check for Ctrl+V, but the real issue is What in the world were you trying to do with that code????
 
Share this answer
 
v2
Here try this one in the KeyPress Event of the Textbox.

Here is for Letters

Try
If (e.KeyChar < "a" AndAlso e.KeyChar < "A" OrElse e.KeyChar > "z" AndAlso e.KeyChar > "Z") AndAlso e.KeyChar <> ControlChars.Back Then
ErrorProvider1.SetError(txtfname, "Opps!... Letters Only")
e.Handled = True
End If
Catch ex As Exception
End Try


Here is for Numbers
Try
If (e.KeyChar < "0" AndAlso e.KeyChar < "9") AndAlso e.KeyChar <> ControlChars.Back Then
ErrorProvider1.SetError(txtfname, "Opps!... Numbers Only")
e.Handled = True
End If
Catch ex As Exception
End Try
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900