PDA

View Full Version : right-click in textbox



av8tordude
07-26-2011, 07:11 PM
I have a userform textbox. Is it not possible to right-click in a textbox so I can use the paste option from the menu that appears? If so, how do I enable this feature. I can right-click on the sheet, but can't do it in the userform textbox. I know I can use CTRL+V, but not all user are computer savvy. Thanks

mikerickson
07-27-2011, 12:39 AM
There is no RightClick event for a userform's textbox, but you can use the MouseUp event

Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
If Button = 2 Then
MsgBox "You right clicked"
End If
End Sub

To paste to the textbox, one needs to use a DataObject object.

Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
Dim ClipboardData As New DataObject
Dim strClipboard As String

If Button = 2 Then
ClipboardData.GetFromClipboard
strClipboard = ClipboardData.GetText(1)
For i = 1 To Len(strClipboard)
Cells(i, 1) = Mid(strClipboard, i, 1)
Next i
If strClipboard <> vbNullString Then
strClipboard = Left(strClipboard, InStr(strClipboard & Chr(3) & Chr(197), Chr(3) & Chr(197)))
TextBox1.Text = strClipboard
End If
End If
End Sub

Kenneth Hobs
07-27-2011, 05:59 AM
Users that don't know how to paste with Ctrl+V or the Windows standard Shift+Ins will probably have lots of problems. You could add a label to give them a tip or a controltiptext or a drop option or a combination of those options. Here is the controltiptext and drop option example.
'Textbox drop example: http://peltiertech.com/WordPress/refedit-control-alternative/
Private Sub UserForm_Initialize()
TextBox1.DropButtonStyle = fmDropButtonStyleArrow
TextBox1.ShowDropButtonWhen = fmShowDropButtonWhenFocus
End Sub

Private Sub TextBox1_DropButtonClick()
Dim rc As Integer
rc = MsgBox("Paste Text?", vbQuestion + vbYesNo, "Paste Option")
If rc = vbYes Then TextBox1.Paste
TextBox2.SetFocus
TextBox1.SetFocus
End Sub

Private Sub TextBox1_Enter()
TextBox1.ControlTipText = "Press Ctrl+V to Paste"
End Sub