|
-
Mar 8th, 2007, 01:59 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Locking a textbox.
I've got a textbox on my form and inside that textbox is a value.
I have it set up so that the cursor is hidden whenever the user clicks on a given textbox. I also have it setup so that they cannot type anything into the textbox.
I have two issues still though.
1) The user can highlight the text with the mouse, then click the delete key.
2) The user can click in the textbox and even though the cursor doesn't show up it is there, so the user can hit the delete key and erase the textbox contents that way as well.
I could simply set ENABLED = FALSE but I want to allow clicking and OLE Drag Drop....just no typing or deleting.
Any ideas?
-
Mar 8th, 2007, 02:05 PM
#2
Re: Locking a textbox.
Try
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = 0
End Sub
-
Mar 8th, 2007, 02:10 PM
#3
Re: Locking a textbox.
What about,
Code:
Text1.Locked = True
-
Mar 8th, 2007, 02:12 PM
#4
Thread Starter
Fanatic Member
Re: Locking a textbox.
 Originally Posted by Hack
Try
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = 0
End Sub
I can still click into the cell with the mouse and then hit the delete key. The KEYPRESS event fires when I hit a letter, but when I hit the delete key the event doesn't even fire.
 Originally Posted by iPrank
What about,
Code:
Text1.Locked = True
I thought of that, but won't that disable click and drag drop events as well?
Last edited by The_Grudge; Mar 8th, 2007 at 02:18 PM.
-
Mar 8th, 2007, 02:30 PM
#5
Re: Locking a textbox.
Try this:
vb Code:
Option Explicit
Private Sub Form_Load()
Text1.Locked = True
Text1.OLEDropMode = 1 'Manual
End Sub
Private Sub Text1_Click()
Debug.Print "Click"
End Sub
Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.SelText = Data.GetData(1)
End Sub
Private Sub Text1_OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer)
State = 1
'need more check here to verify the data is text
End Sub
-
Mar 8th, 2007, 02:36 PM
#6
Thread Starter
Fanatic Member
Re: Locking a textbox.
I used the KEYDOWN event instead...that seems to catch all keypresses.
Even 46/delete key.
-
Mar 8th, 2007, 02:41 PM
#7
Re: [RESOLVED] Locking a textbox.
But unless you are subclassing it, your users can delete text from the right-click context menu.
-
Mar 8th, 2007, 02:43 PM
#8
Thread Starter
Fanatic Member
Re: [RESOLVED] Locking a textbox.
 Originally Posted by iPrank
But unless you are subclassing it, your users can delete text from the right-click context menu.
iPrank...I already had the right-click menus blocked so it's working.
Thanks for the input though.
-
Mar 8th, 2007, 04:29 PM
#9
Re: [RESOLVED] Locking a textbox.
How about a simplier way:
VBCode Code:
'Add a Frame to your form, Command Button and then
'cut and paste the textbox into the frame.
'Then run the following code
Private Sub Command1_Click()
Call LockFrame(Not Frame1.Enable)
'This will toggle the Enable Value
End Sub
Private Sub LockFrame(blnLockStat as Boolean)
Frame1.Enable = blnLockStat
Text1.ForeColor = IIF(blnLockStat, vbBlack, vbBlue)
'When the frame is locked (Frame1.Enable = False) the Text
'in the textbox will be blue and the user will not be able to change
'the data.
End Sub
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Mar 8th, 2007, 06:01 PM
#10
Re: [RESOLVED] Locking a textbox.
You might find this interesting:
Code:
Private Sub TXTextControl1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 3 Then KeyAscii = 0
End Sub
Now you can still select and do copy and paste into Notepad or some other text box that has no restrictions. However, the contents of your text box will remain intact.
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
|