On Mar 20, 2:07*am, kimiraikkonen <kimiraikkone...@gmail.comwrote:
John,
For your purpose, there's a built-in template does this. Insert
MenuStrip control. Right click -Insert Standard Items.
Now the items that you're looking for are created, now just you need
to write codes for each item.
Which doesn't help him (or me) that much. This is what I came up with
(most leeched from the MSDN example but hacked to work on any textbox
for example. For other controls add code as needed):
Private Function GetControl() As Control
Dim cControl As ContainerControl, aControl As Control
aControl = Me.ActiveControl
Do
If TypeOf aControl Is TextBox Then Exit Do
' Cast to ContainerControl to access next level
ActiveControl
cControl = CType(aControl, ContainerControl)
aControl = cControl.ActiveControl
Loop
If TypeOf aControl Is TextBox Then
GetControl = aControl
Else
GetControl = Nothing
End If
End Function
Private Sub Menu_Copy(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuCopy.Click, btnCopy.Click
Dim tControl As TextBox
' Ensure that text is selected in the text box.
tControl = CType(GetControl(), TextBox)
If tControl.SelectionLength 0 Then
' Copy the selected text to the Clipboard.
tControl.Copy()
End If
End Sub
Private Sub Menu_Cut(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuCut.Click, btnCut.Click
Dim tControl As TextBox
' Ensure that text is currently selected in the text box.
tControl = CType(GetControl(), TextBox)
If tControl.SelectedText <"" Then
' Cut the selected text in the control and paste it into
the Clipboard.
tControl.Cut()
End If
End Sub
Private Sub Menu_Paste(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuPaste.Click, btnPaste.Click
Dim tControl As TextBox
' Determine if there is any text in the Clipboard to paste
into the text box.
tControl = CType(GetControl(), TextBox)
If Clipboard.GetDataObject().GetDataPresent(DataForma ts.Text)
= True Then
' Determine if any text is selected in the text box.
If tControl.SelectionLength 0 Then
' Ask user if they want to paste over currently
selected text.
If MessageBox.Show("Do you want to paste over current
selection?", _
"Cut Example", MessageBoxButtons.YesNo) =
DialogResult.No Then
' Move selection to the point after the current
selection and paste.
tControl.SelectionStart = tControl.SelectionStart
+ _
tControl.SelectionLength
End If
End If
' Paste current text in Clipboard into text box.
tControl.Paste()
End If
End Sub
Private Sub Menu_Undo(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuUndo.Click
Dim tControl As TextBox
tControl = CType(GetControl(), TextBox)
' Determine if last operation can be undone in text box.
If tControl.CanUndo = True Then
' Undo the last operation.
tControl.Undo()
' Clear the undo buffer to prevent last action from being
redone.
tControl.ClearUndo()
End If
End Sub