Results 1 to 29 of 29

Thread: Lock textbox but can still copy from it [solved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Utah
    Posts
    397

    Lock textbox but can still copy from it [solved]

    I have a text box that is locked, but i cant ctrl+c the text in it.
    how can i lock a text box but still be able to copy from it?
    Last edited by Narfy; Dec 20th, 2003 at 10:26 AM.

  2. #2

  3. #3
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    You could, instead, subclass it and kill any messages that will add to the text box. That would be the cleanest, but you could also use something like this:

    VB Code:
    1. 'You need a command button named cmdLockUnlock and
    2. ' a textbox named txtText to use this.
    3.  
    4. Dim bUnlocked As Boolean
    5. Dim OldText As String
    6.  
    7.  
    8. Private Sub cmdLockUnlock_Click()
    9. bUnlocked = Not bUnlocked
    10.  
    11. If bUnlocked = False Then
    12.     txtText.BackColor = &H8000000F
    13. Else
    14.     txtText.BackColor = vbWhite
    15. End If
    16. End Sub
    17.  
    18. Private Sub txtText_Change()
    19. If bDontProcess_txtText_Change = True Then Exit Sub
    20.  
    21. OldText = txtText.Text
    22. DoEvents
    23. If txtText.Text = OldText And bUnlocked = False Then Exit Sub
    24.  
    25. bDontProcess_txtText_Change = True
    26. txtText.Text = OldText
    27. bDontProcess_txtText_Change = False
    28. End Sub
    29.  
    30. Private Sub txtText_KeyPress(KeyAscii As Integer)
    31. If bUnlocked = False Then
    32.     If KeyAscii = 3 Then Exit Sub 'If it equals ^C then exit sub
    33.    
    34.     KeyAscii = 0
    35. End If
    36. End Sub

    Edit: That's if you meant Enabled. If you did mean locked, then it should do that already.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Utah
    Posts
    397
    it is locked,

    I can copy it by right clicking the selected text and clicking copy
    but i want my users to be able to use ctrl+c on it.

  5. #5

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Utah
    Posts
    397
    you cant use ctrl+c

  7. #7
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068
    Just make it so when they double click it, they get it on the clipboard
    VB Code:
    1. Private Sub Text1_DblClick()
    2.    Clipboard.Clear
    3.    Clipboard.SetText Text1.Text
    4. End Sub

  8. #8
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    And you can hide the default right click menu by making your own, but making the menu items Enabled = False

  9. #9
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84
    Or!

    VB Code:
    1. Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     If KeyCode = 17 Then
    3.         If KeyCode = 67 Then
    4.             Clipboard.Clear
    5.             Clipboard.SetText Text1.Text
    6.         End If
    7.     End If
    8. End Sub

    This detects when the Ctrl and C keys have been pressed, and sets the clipboard text
    Anticipation of death is worse than death itself

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Utah
    Posts
    397
    well still want the user to press ctrlc and not just double click it, i like walter's idea but his code doesn't work...

  11. #11
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84
    guess i should of tested it first i'll get back to you on this one
    Anticipation of death is worse than death itself

  12. #12
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84
    OK, give this one a go

    VB Code:
    1. Option Explicit
    2. Dim ctrl As Integer
    3. Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    4.     If KeyCode = 17 Then ctrl = 1
    5.     If KeyCode = 67 Then
    6.         If ctrl = 1 Then
    7.             Clipboard.Clear
    8.             Clipboard.SetText Text1.Text
    9.         End If
    10.     End If
    11. End Sub
    12. Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    13.     If KeyCode = 17 Then ctrl = 0
    14. End Sub
    Anticipation of death is worse than death itself

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Utah
    Posts
    397
    hmmm, still doesn't work

  14. #14
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84
    are you sure. hmm, i tested it twice...

    yup, tested it for a third time. definately works here
    Anticipation of death is worse than death itself

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Utah
    Posts
    397
    when you are testing it, is your textbox locked?

  16. #16
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84
    tested twice unlocked, and twice locked now. if it still doesn't work, can you post your exact code?
    Anticipation of death is worse than death itself

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Utah
    Posts
    397
    nevermind, it works perfect. thanks walter

  18. #18
    Lively Member
    Join Date
    Aug 2002
    Location
    England
    Posts
    84
    welcome
    Anticipation of death is worse than death itself

  19. #19
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Use the Shift argument of the KeyDown event to check if the Ctrl, Shift or Alt keys are down as well. No need to use the extra variable.

    VB Code:
    1. Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    2.  'On Ctrl-C copy Textbox data  to clipboard
    3.   If KeyCode = vbKeyC And (Shift And vbCtrlMask) Then
    4.       Clipboard.Clear
    5.       Clipboard.SetText Text1.Text, vbCFText
    6.   End If
    7. End Sub

  20. #20
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by Narfy
    you cant use ctrl+c
    But my point was that in order to use ctrl+c with a regular textbox you have to double-click the text or drag the mouse over the text which is exactly what you need to do when the textbox is locked.

  21. #21
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Marty, Ctrl-C just does not work on a locked textbox (and it shouldn't). No matter if you select all the text or not.

  22. #22
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    I thought it should

  23. #23
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by brucevde
    Marty, Ctrl-C just does not work on a locked textbox (and it shouldn't). No matter if you select all the text or not.
    Try it. It's very simple to show that it does. Just put two textboxes on a form, one locked and the other not. Double-click the locked one, do Ctrl-c and paste it to the unlocked one. BTW, locked just means you can't change it.

  24. #24
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    As marty said, locked just makes it read-only, i can copy text with Ctrl+C on a locked textbox

  25. #25
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Hmmm. I think there is a bug here. Try this.

    Ctrl-C the contents of Text2 (the unlocked one).
    Ctrl-C the contents of Text1
    Now try to paste with Ctrl-V back into Text2.

    It seems if the clipboard already contains data, it is not replaced by Ctrl-C from a locked textbox.

    At least that is how it works on my PC.

  26. #26
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Saying that bruce, sometimes i cant right Ctrl+C some text boxes and copy it, even when its not locked! Make sure you did click it right, especially if you have been to the pub

  27. #27
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    heh heh. Its only 10am here. I don't hit the pubs till 11.

    It works using the mouse but not keyboard (all the time).

  28. #28
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    How odd, its temporemental for me, sometimes it does, sometimes it doesnt. But this happens even on unlocked textboxes, such as Notepad

  29. #29
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    I should know better by now than to test things on my old PC. Man I need to upgrade or else get rid of this PII 300mhz machine.

    Forget everything I said, I found this KB article.

    FIX: CTRL+C Does Not Copy From a Locked TextBox

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