0

I'm using Visual Studio 2013 and writing in VB.NET. I am creating a project for an internship at school. Right now in one of my forms I have it where you must type in your name. Here's some pertinent information.

For the TextBox I do have Property of ShortcutsEnabled = False so the user cannot copy and paste into the textbox if this is my problem then I'll just have to go without it. Now, I did this so it would make my errorchecking Sub simpler and more condensed so I wouldn't have to check for IsNumeric. The reason why I wouldn't have to check is because in the textboxes' KEYPRESS (not keydown) event I have it restricting what input is allowed.

 Private Sub Nametxt_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Nametxt.KeyPress

    Dim AllowedChars As String = "abcdefghijklmnopqrstuvwxyz" 'The characters that you are allowed to enter

    If e.KeyChar <> ControlChars.Back And _
       ModifierKeys <> Keys.Shift _
       And e.KeyChar <> Chr(32) Then

        'e.KeyChar <> ControlChars.Back allows the user to use the backspac
        'ModifierKeys <> Keys.Shift allows the user to use the shift key so they can enter Capital letters
        'e.KeyChar <> Chr(32) allows the user to use the space bar so they can enter their First and Last name.

        If AllowedChars.IndexOf(e.KeyChar) = -1 Then
            e.Handled = True

        End If

Now, what I am trying to do is allow the user to also press "Ctrl+A" So that they can select all of the text in the box for convenience. Do remember that I have the textboxes' property of ShortcutsEnabled = False

This did not work:

If e.KeyChar <> ControlChars.Back And _
   ModifierKeys <> Keys.Shift And _
   e.KeyChar <> Chr(32) And _

New line --> (e.KeyChar <> Chr(61) AndAlso ModifierKeys <> Keys.Control) Then

End If

Note: Because I am using the KEYPRESS event and not the keydown event, the only e.key code that is viable is the e.keychar ||||| e.keycode and e.keyvalue are unusable in this event handler

LW001
  • 2,452
  • 6
  • 27
  • 36
Richard Paulicelli
  • 129
  • 3
  • 6
  • 23
  • why not just validate the input when they hit an OK button or leave the textbox rather than trying to test, validate and control every keystroke? – Ňɏssa Pøngjǣrdenlarp Apr 03 '14 at 16:24
  • So that way there's no real possibility of the them bypassing or circumventing it. It makes it easier in the long run (at least for me) when looking through the code and the ErrorChecking is half as long because your checking the keystrokes. – Richard Paulicelli Apr 03 '14 at 16:28
  • 1
    You cannot have it both ways, you need to set ShortcutsEnabled back to True to detect Ctrl+A. Which only works if Multiline = True, for a single-line TextBox you need to implement the KeyDown event to detect it, call SelectAll(). You can disable Paste by checking for e.KeyChar > ChrW(32) but it will not disable the context menu (press the right mouse button). Fixing *that* requires deriving your own control from TextBox so you can override WndProc(). This starts to push your graduation date back by several months. – Hans Passant Apr 03 '14 at 16:31
  • Okay lol Thank you @HansPassant I figured that because I have shortcuts disabled that was the cause. I just don't want the user to copy and paste. I appreciate your explanation. It is very thorough :) – Richard Paulicelli Apr 03 '14 at 16:34
  • another way to 'disable' the default textbox context menu is to assign your own to it which does not include Cut/Copy/Paste. the menu can even be empty – Ňɏssa Pøngjǣrdenlarp Apr 03 '14 at 16:43

0 Answers0