Results 1 to 18 of 18

Thread: Copy locked textbox

  1. #1

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Copy locked textbox

    Is there an easy way to allow a user to copy text from a locked textbox? I need to display an ID number. The user may well want to copy the number to the clipborad though. I can add a keypress event to check for ctnl+c, but I'm not sure how to enable to Copy option in the right-click dropdown. Does anyone have code for this, or have a better control that would allow me to display text that can be copied but not changed?

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Copy locked textbox

    But in a textbox, if you have the locked property set to true, the right-click-popup-menu does have the copy option What did I misunderstand?


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: Copy locked textbox

    Quote Originally Posted by manavo11
    But in a textbox, if you have the locked property set to true, the right-click-popup-menu does have the copy option What did I misunderstand?
    It does have the Copy option, but it is disabled. I want it enabled.

  4. #4
    Frenzied Member ice_531's Avatar
    Join Date
    Aug 2002
    Location
    Sitting w/ Bob Status: -Next -To- Null- Friend: Philip
    Posts
    1,152

    Re: Copy locked textbox

    uhh

    When i have a locked textbox you CAN still copy text from it
    :::`DISCLAIMER`:::
    Do NOT take anything i have posted to be truthful in any way, shape or form.
    Thank You!

    --------------------------------
    "Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
    "Finaly I can look as gay as I want..." - NoteMe
    Languages: VB6, BASIC, Java, C#. C++

  5. #5
    Frenzied Member ice_531's Avatar
    Join Date
    Aug 2002
    Location
    Sitting w/ Bob Status: -Next -To- Null- Friend: Philip
    Posts
    1,152

    Re: Copy locked textbox

    It's not disabled. you can right-click Copy and then go to another window and ctrl + v ..ull paste the textboxes text

    maybe u have the textbox enabled property set to false?
    :::`DISCLAIMER`:::
    Do NOT take anything i have posted to be truthful in any way, shape or form.
    Thank You!

    --------------------------------
    "Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
    "Finaly I can look as gay as I want..." - NoteMe
    Languages: VB6, BASIC, Java, C#. C++

  6. #6

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: Copy locked textbox

    I get it now. (I've atually benn frustrated by this for years, because it "seems" to sometimes work and sometimes not).

    The the whole text is selected, it can be copied. Simple as that. So I did this:

    VB Code:
    1. Private Sub txtText_KeyUp(KeyCode As Integer, Shift As Integer)
    2.     ' Copy text for cntl+c.
    3.     If Shift = vbCtrlMask And KeyCode = 67 Then
    4.         Clipboard.Clear
    5.         Clipboard.SetText txtText.Text
    6.     End If
    7. End Sub
    8.  
    9. Private Sub txtText_MouseDown(Button As Integer, _
    10.       Shift As Integer, X As Single, Y As Single)
    11.     ' Select text.
    12.     txtText.SelStart = 0
    13.     txtText.SelLength = Len(txtText.Text)
    14. End Sub

    Works like buttered green beans. Thanks!

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Copy locked textbox

    I think you're just made it hard for yourself, WH - it's a normal windows functionality: before you can copy anything you'll have to select it first regardless. So, let the user decide what they want to do - if they need to copy some text then they are going to have to select it and then press ctrl+c or right click and ...

  8. #8

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: Copy locked textbox

    But when the text is only partially selected, cntl+c doesn't copy. Acutally, cntl+c doesn't seem to copy at all when the text box is locked. So I HAVE to have a Key event to copy from a locked textbox. Right?
    Last edited by WorkHorse; Jan 6th, 2005 at 11:43 PM.

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Copy locked textbox

    No, wrong.
    LOCKED doesn't disable COPY at all unless your textbox was subclassed somehow in your program so it affected this functionality. Selecting all text or just part of it will automatically activate that (COPY) menu item.

  10. #10

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: Copy locked textbox

    Quote Originally Posted by RhinoBull
    No, wrong.
    LOCKED doesn't disable COPY at all unless your textbox was subclassed somehow in your program so it affected this functionality. Selecting all text or just part of it will automatically activate that (COPY) menu item.
    Doesn't work for me. Try this. Start VB6 and make a new Standard EXE. Add a text box (it should be named Text1 and have Text property of Text1). Set Text1 Locked property to True.

    Hit Start to run the app. Highlight the charcters "xt" in "Text1" of the textbox. Press Ctrl+C. Open notepad. Hit Ctrl+V (or Shift+Insert or use the menu to paste). Does it put "xt" in Notepad? I don't get "xt" when I do this. Notepad puts in whatever junk I had in the clipboard before before I started the app.

  11. #11
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Copy locked textbox

    It works just like in any other windows based applications: copies selected text and paste it onto whatever ... Not sure why it isn't working for you ???

  12. #12

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: Copy locked textbox

    Quote Originally Posted by RhinoBull
    It works just like in any other windows based applications: copies selected text and paste it onto whatever ... Not sure why it isn't working for you ???
    Serious? What you said about the drop down is true. The drop down copies any or all selected text. But Ctrl+C does not copy AT ALL for me when the texbox is locked (unless I code a Key event).

  13. #13
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Copy locked textbox

    Hey Guys,

    WH, it seems to not only disable the Ctl+C function, but the drop down menu is also limited (to Copy) aswell.

    Edit: It sorta makes sense tho.





    Bruce.

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Copy locked textbox

    I got Ctl+C working and the dropdown has Copy + Paste enabled,
    but of course the paste doesn't work since its locked.

    Edit: Running SP6 could that be why?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: Copy locked textbox

    Quote Originally Posted by Bruce Fox
    Hey Guys,

    WH, it seems to not only disable the Ctl+C function, but the drop down menu is also limited (to Copy) aswell.

    Edit: It sorta makes sense tho.
    The only other options are Cut, Paste, Delete, and Undo--which wouldn't make sense for a locked box. Anyway, to cover the Ctrl+C problem and cover some of the problems my users have been having (which appears now to be attempting to copy when no text is selected) I've decided to use this:

    VB Code:
    1. Private Sub txtText_KeyUp(KeyCode As Integer, Shift As Integer)
    2.  
    3.     ' Check for Ctrl.
    4.     If Shift = vbCtrlMask Then
    5.  
    6.         ' Check KeyCode.
    7.         Select Case KeyCode
    8.  
    9.             Case 65 ' Ctrl+A
    10.            
    11.                 ' Select all text.
    12.                 TextBoxSelectAll txtText
    13.  
    14.             Case 67 ' Ctrl+C
    15.            
    16.                 ' Copy selected text or copy all text
    17.                 ' if no text is selected.
    18.                 If txtTextBox.SelLength = 0 Then
    19.                     TextBoxCopyAll txtText
    20.                 Else
    21.                     TextBoxCopySelected txtText
    22.                 End If
    23.  
    24.         End Select
    25.     End If
    26.  
    27. End Sub
    28.  
    29. Private Sub txtText_MouseDown(Button As Integer, Shift As Integer, _
    30.                               X As Single, Y As Single)
    31.  
    32.     ' check for right button click.
    33.     If Button = 2 And txtText.SelLength = 0 Then
    34.    
    35.         ' Select all text.
    36.         TextBoxSelectAll txtText
    37.    
    38.     End If
    39.  
    40. End Sub
    41.  
    42. Public Sub TextBoxSelectAll(ByVal txtTextBox As TextBox)
    43.  
    44.     ' Select all text.
    45.     txtTextBox.SelStart = 0
    46.     txtTextBox.SelLength = Len(txtText.Text)
    47.  
    48. End Sub
    49.  
    50.  
    51. Public Sub TextBoxCopyAll(ByVal txtTextBox As TextBox)
    52.  
    53.     ' Copy all text to clipboard.
    54.     Clipboard.Clear
    55.     Clipboard.SetText txtTextBox.Text
    56.  
    57. End Sub
    58.  
    59. Public Sub TextBoxCopySelected(ByVal txtTextBox As TextBox)
    60.  
    61.     ' Copy selected text to clipboard.
    62.     Clipboard.Clear
    63.     Clipboard.SetText txtTextBox.SelText
    64.  
    65. End Sub

  16. #16

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: Copy locked textbox

    Here is a trickier question. Why does the Sub TextBoxSelectAll work when the TextBox is passed ByVal? Shouldn't it only affect the the passed TextBox if it were passed ByRef?

  17. #17
    Registered User HellRaider's Avatar
    Join Date
    Sep 2003
    Posts
    70

    Re: Copy locked textbox

    Hello
    Don't worry i've got the exact problem. I think its because it might stop the c button when you press ctrl c so it only regesters the ctrl. I think.

  18. #18
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Copy locked textbox

    Quote Originally Posted by WorkHorse
    Here is a trickier question. Why does the Sub TextBoxSelectAll work when the TextBox is passed ByVal? Shouldn't it only affect the the passed TextBox if it were passed ByRef?
    Objects are always passed ByRef regardless.

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