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?
Printable View
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?
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.
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.
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.
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.
you cant use ctrl+c
Just make it so when they double click it, they get it on the clipboard :D
VB Code:
Private Sub Text1_DblClick() Clipboard.Clear Clipboard.SetText Text1.Text End Sub
And you can hide the default right click menu by making your own, but making the menu items Enabled = False;)
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 :)
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...
guess i should of tested it first :lol: i'll get back to you on this one :D
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
hmmm, still doesn't work
are you sure. hmm, i tested it twice...
yup, tested it for a third time. definately works here :confused:
when you are testing it, is your textbox locked?
tested twice unlocked, and twice locked now. if it still doesn't work, can you post your exact code?
nevermind, it works perfect. thanks walter
welcome :thumb:
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
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.Quote:
Originally posted by Narfy
you cant use ctrl+c
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.
I thought it should:confused:
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.Quote:
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.
As marty said, locked just makes it read-only, i can copy text with Ctrl+C on a locked textbox
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.
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;)
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).
How odd, its temporemental for me, sometimes it does, sometimes it doesnt. But this happens even on unlocked textboxes, such as Notepad:( :confused:
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