Search Tools Links Login

Copybox/Copylabel


Visual Basic 6, or VB Classic

This snippet creates an uneditable textbox/label whose contents can be copied to the clipboard.
This is just like the textboxes in the File properties of Windows (open Windows Explorer, right-click on a file and select PROPERTIES,
you'll see that several fields here can be copy/pasted although they cannot be modified).
Straightforward and commented code.

Original Author: tilleul

Assumptions

This is very basic code, very easy to understand.

Code

' ##################################################################
' CopyBox/CopyLabel: (c) 2002 Tilleul
' =======
' This snippet creates an uneditable textbox/label whose contents can
' be copied to the clipboard.
' This is just like the textboxes in the File properties of Windows
' (open Windows Explorer, right-click on a file and select PROPERTIES,
' you'll see that several fields here can be copy/pasted although they
' cannot be modified).
'
' Usage:
' 1- open a new VB project
' 2- on form1, draw a textbox (name=text1)
' 3- on form1, draw a second textbox (name doesn't matter)
' 4- copy/paste the code
' ##################################################################

Private Sub Form_Load()
' some text to test the snippet
Text1.Text = "This textbox' contents cannot be modified but you can copy/paste any part of it in the one below"
' no border
Text1.BorderStyle = 0
' flat (not 3D)
Text1.Appearance = 0
' backcolor is the same as your windows default backcolor
Text1.BackColor = vbButtonFace
End Sub

Private Sub Text1_Keydown(KeyCode As Integer, Shift As Integer)
If (Shift = vbCtrlMask And (KeyCode = vbKeyInsert Or KeyCode = vbKeyC)) And _
    (Text1.SelLength <> 0) Then
    ' if user presses CTRL-C or CTRL-INS (Windows keyboard shortcuts to copy to clipboard)
    ' and if there is selected text, then copy this text
    Clipboard.SetText Text1.SelText
ElseIf KeyCode <> vbKeyLeft And KeyCode <> vbKeyRight And KeyCode <> vbKeyUp And KeyCode <> vbKeyDown Then
  ' if not an arrow key then cancel
  KeyCode = 0
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
' whatever key is pressed, cancel it
KeyAscii = 0
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 110 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.