CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2005
    Posts
    158

    [RESOLVED] disabling text box

    I have a text box that I want to mostly disable. I would like the user to be able to copy the text, but that's all.
    Here's the code for the keyDown event:
    Code:
    e.Handled = True
    If e.KeyCode = Keys.ControlKey + Keys.C Then
      System.Windows.Forms.Clipboard.SetText(rtbErrorList.Text)
    End If
    I'm never getting into the If when I press ^C. Any suggestions?

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: disabling text box

    Try e.KeyData instead of E.KeyCode

  3. #3
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    149

    Re: disabling text box

    If Only copying the text in the text box no other operation is required you can set ReadOnly property to True, Still you can copy the text.

    I think you are looking for code something like below:
    Code:
        Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            If My.Computer.Keyboard.CtrlKeyDown Then
                Select Case e.KeyCode
                    Case Keys.C
                    Case Keys.V
                    Case Keys.Right
                    Case Keys.Left
                    Case Else
                        e.Handled = True
                End Select
            Else
                Select Case e.KeyCode
                    Case Keys.Right
                    Case Keys.Left
                    Case Else
                        e.Handled = True
                End Select
    
            End If
        End Sub
    
        Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            e.Handled = True
        End Sub
    Last edited by ComITSolutions; February 24th, 2010 at 06:34 AM.
    Encourage the efforts of fellow members by rating

    Lets not Spoon Feed and create pool of lazy programmers

    - ComIT Solutions

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: disabling text box

    The keydata should work also. I tested it under VS2005 and KeyData was = Keys.Control + Keys.C when Control + C was pressed

  5. #5
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    149

    Re: disabling text box

    Quote Originally Posted by DataMiser View Post
    The keydata should work also. I tested it under VS2005 and KeyData was = Keys.Control + Keys.C when Control + C was pressed
    Yes, I agree. many people wouldn't have explored My.Computer, I just wanted freshers, newcomers and others to know about it.
    Encourage the efforts of fellow members by rating

    Lets not Spoon Feed and create pool of lazy programmers

    - ComIT Solutions

  6. #6
    Join Date
    Oct 2005
    Posts
    158

    Re: disabling text box

    Wow, I can't believe I missed that (well can't is such a strong word ). Read-only is exactly what I was looking for. Thank you folks also for the heads up about the keyData.

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