Results 1 to 10 of 10

Thread: [RESOLVED] Locking a textbox.

  1. #1

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Resolved [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?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Locking a textbox.

    Try
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    KeyAscii = 0
    End Sub

  3. #3
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Locking a textbox.

    What about,
    Code:
    Text1.Locked = True
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  4. #4

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: Locking a textbox.

    Quote 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.

    Quote 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.

  5. #5
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Locking a textbox.

    Try this:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Text1.Locked = True
    5.     Text1.OLEDropMode = 1 'Manual
    6. End Sub
    7.  
    8. Private Sub Text1_Click()
    9.     Debug.Print "Click"
    10. End Sub
    11.  
    12. Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    13.     Text1.SelText = Data.GetData(1)
    14. End Sub
    15.  
    16. 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)
    17.     State = 1
    18.     'need more check here to verify the data is text
    19. End Sub
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  6. #6

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: Locking a textbox.

    I used the KEYDOWN event instead...that seems to catch all keypresses.
    Even 46/delete key.

  7. #7
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: [RESOLVED] Locking a textbox.

    But unless you are subclassing it, your users can delete text from the right-click context menu.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  8. #8

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: [RESOLVED] Locking a textbox.

    Quote 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.

  9. #9
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [RESOLVED] Locking a textbox.

    How about a simplier way:

    VBCode Code:
    1. 'Add a Frame to your form, Command Button and then
    2. 'cut and paste the textbox into the frame.
    3. 'Then run the following code
    4.  
    5. Private Sub Command1_Click()
    6.       Call LockFrame(Not Frame1.Enable)
    7.         'This will toggle the Enable Value
    8. End Sub
    9.  
    10. Private Sub LockFrame(blnLockStat as Boolean)
    11.  
    12.      Frame1.Enable = blnLockStat
    13.      Text1.ForeColor = IIF(blnLockStat, vbBlack, vbBlue)
    14.          'When the frame is locked (Frame1.Enable = False) the Text
    15.          'in the textbox will be blue and the user will not be able to change
    16.          'the data.
    17. 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."


  10. #10
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

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