|
-
Dec 19th, 2003, 08:01 PM
#1
Thread Starter
Hyperactive Member
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.
-
Dec 19th, 2003, 08:27 PM
#2
Are you talking about locked or not enabled? I ask because you should be able to copy from a locked textbox by double-clicking the text and then Ctrl+c.
-
Dec 19th, 2003, 08:30 PM
#3
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:
'You need a command button named cmdLockUnlock and
' a textbox named txtText to use this.
Dim bUnlocked As Boolean
Dim OldText As String
Private Sub cmdLockUnlock_Click()
bUnlocked = Not bUnlocked
If bUnlocked = False Then
txtText.BackColor = &H8000000F
Else
txtText.BackColor = vbWhite
End If
End Sub
Private Sub txtText_Change()
If bDontProcess_txtText_Change = True Then Exit Sub
OldText = txtText.Text
DoEvents
If txtText.Text = OldText And bUnlocked = False Then Exit Sub
bDontProcess_txtText_Change = True
txtText.Text = OldText
bDontProcess_txtText_Change = False
End Sub
Private Sub txtText_KeyPress(KeyAscii As Integer)
If bUnlocked = False Then
If KeyAscii = 3 Then Exit Sub 'If it equals ^C then exit sub
KeyAscii = 0
End If
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.
-
Dec 19th, 2003, 09:08 PM
#4
Thread Starter
Hyperactive Member
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.
-
Dec 19th, 2003, 09:44 PM
#5
I don't understand. What is it concerning copying that you can't do in a locked textbox that you can do it an unlocked textbox? I don't see any differences.
-
Dec 19th, 2003, 10:15 PM
#6
Thread Starter
Hyperactive Member
-
Dec 20th, 2003, 02:24 AM
#7
Frenzied Member
Just make it so when they double click it, they get it on the clipboard 
VB Code:
Private Sub Text1_DblClick()
Clipboard.Clear
Clipboard.SetText Text1.Text
End Sub
-
Dec 20th, 2003, 07:02 AM
#8
Supreme User
And you can hide the default right click menu by making your own, but making the menu items Enabled = False
-
Dec 20th, 2003, 07:49 AM
#9
Lively Member
Or!
VB Code:
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 17 Then
If KeyCode = 67 Then
Clipboard.Clear
Clipboard.SetText Text1.Text
End If
End If
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
-
Dec 20th, 2003, 09:59 AM
#10
Thread Starter
Hyperactive Member
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...
-
Dec 20th, 2003, 10:05 AM
#11
Lively Member
guess i should of tested it first i'll get back to you on this one
Anticipation of death is worse than death itself
-
Dec 20th, 2003, 10:13 AM
#12
Lively Member
OK, give this one a go 
VB Code:
Option Explicit
Dim ctrl As Integer
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 17 Then ctrl = 1
If KeyCode = 67 Then
If ctrl = 1 Then
Clipboard.Clear
Clipboard.SetText Text1.Text
End If
End If
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 17 Then ctrl = 0
End Sub
Anticipation of death is worse than death itself
-
Dec 20th, 2003, 10:19 AM
#13
Thread Starter
Hyperactive Member
-
Dec 20th, 2003, 10:21 AM
#14
Lively Member
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
-
Dec 20th, 2003, 10:24 AM
#15
Thread Starter
Hyperactive Member
when you are testing it, is your textbox locked?
-
Dec 20th, 2003, 10:26 AM
#16
Lively Member
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
-
Dec 20th, 2003, 10:26 AM
#17
Thread Starter
Hyperactive Member
nevermind, it works perfect. thanks walter
-
Dec 20th, 2003, 10:28 AM
#18
Lively Member
welcome
Anticipation of death is worse than death itself
-
Dec 20th, 2003, 10:59 AM
#19
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:
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
'On Ctrl-C copy Textbox data to clipboard
If KeyCode = vbKeyC And (Shift And vbCtrlMask) Then
Clipboard.Clear
Clipboard.SetText Text1.Text, vbCFText
End If
End Sub
-
Dec 20th, 2003, 12:10 PM
#20
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.
-
Dec 20th, 2003, 12:48 PM
#21
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.
-
Dec 20th, 2003, 12:49 PM
#22
Supreme User
I thought it should
-
Dec 20th, 2003, 12:57 PM
#23
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.
-
Dec 20th, 2003, 12:59 PM
#24
Supreme User
As marty said, locked just makes it read-only, i can copy text with Ctrl+C on a locked textbox
-
Dec 20th, 2003, 01:05 PM
#25
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.
-
Dec 20th, 2003, 01:08 PM
#26
Supreme User
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
-
Dec 20th, 2003, 01:14 PM
#27
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).
-
Dec 20th, 2003, 01:16 PM
#28
-
Dec 20th, 2003, 01:31 PM
#29
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|