Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 546
  • Last Modified:

Default Edit Menu Options - Undo, Redo Cut, Copy, Paste etc.

I am writing an application and I want to use the standard edit menu with the standard edit functions (cut, copy paste etc.).  Is there a simple way to set up this menu to work the same as the context menu when right clicking on a text box.

Thanks.
0
marcus_d
Asked:
marcus_d
  • 3
  • 2
1 Solution
 
r_a_j_e_s_hCommented:
no u have to write a code for that.. u have to use clipboard for storing the strings....
0
 
marcus_dAuthor Commented:
Do you know of any examples to get me started?
0
 
RonaldBiemansCommented:
Private Sub Menu_Copy(sender As System.Object, e As System.EventArgs)
    ' Ensure that text is selected in the text box.  
    If textBox1.SelectionLength > 0 Then
        ' Copy the selected text to the Clipboard.
        textBox1.Copy()
    End If
End Sub
 
Private Sub Menu_Cut(sender As System.Object, e As System.EventArgs)
    ' Ensure that text is currently selected in the text box.  
    If textBox1.SelectedText <> "" Then
        ' Cut the selected text in the control and paste it into the Clipboard.
        textBox1.Cut()
    End If
End Sub
 
Private Sub Menu_Paste(sender As System.Object, e As System.EventArgs)
    ' Determine if there is any text in the Clipboard to paste into the text box.
    If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) = True Then
        ' Determine if any text is selected in the text box.
        If textBox1.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.
                textBox1.SelectionStart = textBox1.SelectionStart + _
                    textBox1.SelectionLength
            End If
        End If
        ' Paste current text in Clipboard into text box.
        textBox1.Paste()
    End If
End Sub

Private Sub Menu_Undo(sender As System.Object, e As System.EventArgs)
    ' Determine if last operation can be undone in text box.  
    If textBox1.CanUndo = True Then
        ' Undo the last operation.
        textBox1.Undo()
        ' Clear the undo buffer to prevent last action from being redone.
        textBox1.ClearUndo()
    End If
End Sub
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
marcus_dAuthor Commented:
Thank you, because my forms are made up of a number of different textboxes, do I have to write individual code for each Textbox?
0
 
RonaldBiemansCommented:
no, you can get textbox in which the menuitem was pressed like, now it will work for every textbox this contextmeun was attachted to

Private Sub Menu_Copy(sender As System.Object, e As System.EventArgs) handles mnuItem1.click
        ' Ensure that text is selected in the text box.  
        If CType(CType(sender, MenuItem).GetContextMenu.SourceControl, TextBox).SelectionLength > 0 Then
            ' Copy the selected text to the Clipboard.
            CType(CType(sender, MenuItem).GetContextMenu.SourceControl, TextBox).Copy()
        End If
    End Sub
0
 
marcus_dAuthor Commented:
Thank you, that is exactly what I was looking for allbeit that my textboxes are on a child window.  I just replaced the code above with :-

Private Sub m_mCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_mCopy.Click
        ' Ensure that text is selected in the text box.
        If CType(ActiveMdiChild.ActiveControl, TextBox).SelectionLength > 0 Then
            'Copy the selected text to the Clipboard
            CType(ActiveMdiChild.ActiveControl, TextBox).Copy()
        End If
    End Sub

One final question.  How do I enable/disable the menu options? eg. disable paste option when the clipboard is empty, disable the cut & copy options when no text is selected.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now