CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2013
    Posts
    1

    Key Stroke Limitations

    What I need is a text box control that only allows users to paste into (either by using the mouse to right click and paste, or by using Ctrl + V). I also need the user to be able to use the backspace button. I have the following code, but I'm having difficulty trying to figure out how to allow the Ctrl + V functionality, as well as the backspace. It DOES currently allow me to only paste, but only by using the mouse. Could anyone offer some assistance?

    Code:
     Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            If e.Control AndAlso e.KeyCode = Keys.V Then
                Dim testVal As Integer
                If Integer.TryParse(Clipboard.GetText, testVal) Then
                    TextBox1.Text = testVal.ToString
    
                End If
            End If
            e.Handled = True
            e.SuppressKeyPress = True
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles TextBox1.TextChanged
            Dim testVal As Integer
            If Not Integer.TryParse(TextBox1.Text, testVal) Then
                TextBox1.Text = ""
    
            End If
        End Sub

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Key Stroke Limitations

    [moved thread]
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Key Stroke Limitations

    Your on the right track.. however i tend to use the TextBox#_KeyPress event... also your trying to handle the events your self rather than filtering out those you dont want to happen...
    So like in this code...
    Code:
        Private Sub TxtAmounts_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtMaxBulk.KeyPress, TxtMaxSale.KeyPress, TxtMinSale.KeyPress, TxtInstFee.KeyPress, TxtFee.KeyPress
            If Not IsDecimalInput(e.KeyChar) Then
                e.Handled = True
            End If
        End Sub
    
            Public Shared Function IsDecimalInput(ByVal Chr As Char) As Boolean
                'Checks if the Char code is valid for a Decimal input (Numbers, Decimal point and control codes only)
                Select Case Asc(Chr)
                    Case 32 To 45
                        Return False
                    Case 47
                        Return False
                    Case 58 To 255
                        Return False
                    Case Else
                        Return True
                End Select
            End Function
    If the key pressed is not a decimal Ie: number, .(decimal point), cursor movement or control key (Bckspc, Del, ent, etc), just set it as handled, there after the built in control code can take care of the keys i allow through..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

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

Featured