Restrict cursor within textbox

ShieBoon

Board Regular
Joined
May 3, 2011
Messages
111
Hi all, i want to restrict the user from clicking the textbox. But i also want the user to see the values of the textbox clearly. So i can't use textbox1.enabled = false. Any ideas?

i'm using excel 2003

Thanks
Shie boon
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
This sets the focus to CommandButton1 when the try to click on TextBox1.

Code:
Private Sub TextBox1_Enter()
CommandButton1.SetFocus
End Sub
 
Upvote 0
Hi Jon, Alpha

To Jon: I don't think so. Because I want to allow users to be able to edit the values, on the same page, when they click an 'edit' button on the user interface.

To Alpha: Good Solution, but i have 33 textboxes. Haha, are there any other alternatives?

Thanks
 
Upvote 0
Hi

Try using the Locked property. It doesn't stop the user from placing the cursor in the control but it effectively makes the control read-only. You can set the text box Locked to True as a default, and then configure your Edit command button to change all of the textbox Locked properties to False. If it is every textbox then you should be able to loop through the controls on the userform and change the property if the TypeName of the control is "TextBox".
 
Upvote 0
Hi Jon,

I've already locked them. Haha, just wanna get rid of the cursor! Thanks for the ideas anws! :)
 
Upvote 0
Follow these steps :

1- Add a Class module to your Project and give it the name of C_LockedTextBox

2- Put in it the code below :

Code:
Option Explicit

Private WithEvents TxtBoxInst As MSForms.TextBox

Private Declare Function HideCaret Lib "User32" _
(ByVal hWnd As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" ( _
pDest As Any, pSrc As Any, _
ByVal ByteLen As Long)

Private oTempObj As Object


'**************************************************
'\Public Property.

Public Property Let GetTextBoxPtr(ByVal Ptr As Long)

    CopyMemory oTempObj, Ptr, 4
    HideCaretAndLock oTempObj
    Set TxtBoxInst = oTempObj

End Property

'***************************************************


'**************************************************
'Private Routines.

Private Sub Class_Terminate()
  CopyMemory oTempObj, 0&, 4
End Sub

Private Sub TxtBoxInst_DblClick _
(ByVal Cancel As MSForms.ReturnBoolean)

    Cancel = True

End Sub

Private Sub TxtBoxInst_KeyPress _
(ByVal KeyAscii As MSForms.ReturnInteger)

    HideCaretAndLock TxtBoxInst
    
End Sub

Private Sub TxtBoxInst_MouseDown _
(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)

    HideCaretAndLock TxtBoxInst
    
End Sub

Private Sub HideCaretAndLock(TextBox As Object)

    TextBox.TabStop = False
    TextBox.Locked = True
    DoEvents
    HideCaret 0

End Sub

'******************************************************
3- Put the code below in the UserForm Module :

Code:
Option Explicit

Private oClass As C_LockedTextBox

Private oCol As New Collection

Private Sub UserForm_Initialize()

    Dim oTxtBox As Control
    
    For Each oTxtBox In Me.Controls
        If TypeOf oTxtBox Is MSForms.TextBox Then
            Set oClass = New C_LockedTextBox
            oClass.GetTextBoxPtr = ObjPtr(oTxtBox)
            oCol.Add oClass
        End If
    Next

End Sub
This should lock all the textboxes (User Interface only) on your Form and still allow the Edit Button to edit the textboxes.
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,508
Members
452,918
Latest member
Davion615

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top