?
Solved

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

Posted on 2005-03-14
6
Medium Priority
?
533 Views
Last Modified: 2012-08-13
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
Comment
Question by:marcus_d
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:r_a_j_e_s_h
ID: 13533947
no u have to write a code for that.. u have to use clipboard for storing the strings....
0
 

Author Comment

by:marcus_d
ID: 13533968
Do you know of any examples to get me started?
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 150 total points
ID: 13534114
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:marcus_d
ID: 13534967
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13535294
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
 

Author Comment

by:marcus_d
ID: 13536037
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question