In the TextBox's MouseDown event handler, see if the right button was pressed. If so, call subroutine DisplayTextPopup.
DisplayTextPopup disables and reenables the TextBox to force Visual Basic to discard the mouse capture it began when the user right clicked. It sets the focus back to the TextBox because that control lost focus when it was disabled.
DisplayTextPopup then examines the TextBox to see if any text is selected and enables or disables the Copy, Cut, and Delete menus appropriately. Finally, DisplayTextPopup pops up the mnuTextCommands menu.
The Cut, Copy, Paste, Delet, and Select All menu items operate on the currently active TextBox.
|
Private Sub Text1_MouseDown(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
If Button = vbRightButton _
Then DisplayTextPopup Text1
End Sub
' Display the text popup for this TextBox.
Private Sub DisplayTextPopup(ByVal txt As TextBox)
' Make VB discard the mouse capture.
txt.Enabled = False
txt.Enabled = True
txt.SetFocus
' Enable appropriate menu items.
' See if anything is selected.
If txt.SelLength > 0 Then
' Text is selected.
mnuCut.Enabled = True
mnuCopy.Enabled = True
mnuDelete.Enabled = True
Else
' No text is selected.
mnuCut.Enabled = False
mnuCopy.Enabled = False
mnuDelete.Enabled = False
End If
' Display the custom menu.
PopupMenu mnuTextCommands
End Sub
' Cut the current TextBox's text
' to the clipboard.
Private Sub mnuCut_Click()
Clipboard.Clear
Clipboard.SetText ActiveControl.SelText
ActiveControl.SelText = ""
End Sub
' Copy the current TextBox's text
' to the clipboard.
Private Sub mnuCopy_Click()
Clipboard.Clear
Clipboard.SetText ActiveControl.SelText
End Sub
' Paste into the current TextBox.
Private Sub mnuPaste_Click()
ActiveControl.SelText = Clipboard.GetText
End Sub
' Delete the selected text.
Private Sub mnuDelete_Click()
ActiveControl.SelText = ""
End Sub
' Select the TextBox's text.
Private Sub mnuSelectAll_Click()
ActiveControl.SelStart = 0
ActiveControl.SelLength = Len(ActiveControl.Text)
End Sub
|