Cut, Copy, Paste code

J

John

Hi

In a winform app I need to provide a menu with Cut, Copy and Paste options.,
What code do I use to accomplish cut, copy and paste features for fields on
a winfrom?

Thanks

Regards
 
C

Cor Ligthert[MVP]

John,

You are probably at the moment the guy who asks the most in these
newsgroups, no problem, however by just copying in your problem in MSDN you
get all the answers you want about that.

When you set your search string than use the trick to set as
well -blogs -forums to prevent that you get all kind of rubish.

http://msdn2.microsoft.com/en-us/default.aspx

When you can not get your answer there, or it is not clear to you, then
place your question here.

Cor
 
K

kimiraikkonen

Hi

In a winform app I need to provide a menu with Cut, Copy and Paste options.,
What code do I use to accomplish cut, copy and paste features for fields on
a winfrom?

Thanks

Regards

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.
 
J

John

Text in fields.

Thanks

Regards

RobinS said:
What are you trying to cut, copy, and paste? The data displayed on the
whole form? Text in a textbox?

RobinS.
GoldMail.com
 
J

John

I only need code for actual Copy, Cut and paste operation. I already have
menus (infragistics) setup.

Thanks

Regards
 
A

Armin Zingler

John said:
Hi

In a winform app I need to provide a menu with Cut, Copy and Paste
options., What code do I use to accomplish cut, copy and paste
features for fields on a winfrom?

I wonder why you want to do something that you don't know how to do it.
I want to build an aircraft, but I don't know how to do, so I don't do
it. If I work in an aircraft building company and am instructed to build
an aircraft and don't know how to do it, I'd wonder if it is the right
job for me. You seem to work in such a company and ask the fellow
workers to do the work for you because you don't know how to do it - so,
what do you get paid for?


Armin
 
K

kevininstructor

Add a context menu to the text control
Add menu items i.e. copy/cut/paste.
Add commands to the menu items

Delete
My.Computer.Clipboard.SetText(TextBox1.SelectedText)
TextBox1.Text = TextBox1.Text.Replace(TextBox1.SelectedText, "")

SelectAll
TextBox1.SelectAll()

Paste
TextBox1.Text = My.Computer.Clipboard.GetText

Undo
TextBox1.Undo()

Cut
My.Computer.Clipboard.SetText(TextBox1.Text)
TextBox1.Text = ""

Also make sure to enable/disable menu items kind like the following
Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles TextBox1.TextChanged

PasteToolStripMenuItem.Enabled = My.Computer.Clipboard.ContainsText
DeleteToolStripMenuItem.Enabled = TextBox1.SelectionLength > 0
CutToolStripMenuItem.Enabled = DeleteToolStripMenuItem.Enabled
CopyToolStripMenuItem.Enabled = DeleteToolStripMenuItem.Enabled
UndoToolStripMenuItem.Enabled = TextBox1.CanUndo
End Sub

Your context menu will replace the normal menu say on a textbox.

Hope this helps,
Kevin
 
J

John

Steve this is fine but I need to implement these in the main menu on the
top.

Thanks

Regards
 
N

N Morrison

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(DataFormats.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
 
R

Raj Kumar

Here is the code, try it out.

Imports Visio = Microsoft.Office.Interop.Visio

Dim visapp As Visio.Application
visapp = GetObject(, "Visio.Application")
//////// Cut
Private Sub CutToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripButton.Click
visapp.ActiveWindow.SelectedText.Cut()
End Sub
////// Copy
Private Sub CopyToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripButton.Click
visapp.ActiveWindow.SelectedText.Copy()

End Sub
/////// Paste
Private Sub PasteToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripButton.Click
visapp.ActiveWindow.SelectedText.Paste()
End Sub



John wrote:

Steve this is fine but I need to implement these in the main menu on the top.
20-Mar-08

Steve this is fine but I need to implement these in the main menu on th
top

Thank

Regards

Previous Posts In This Thread:

Cut, Copy, Paste code
H

In a winform app I need to provide a menu with Cut, Copy and Paste options.
What code do I use to accomplish cut, copy and paste features for fields o
a winfrom

Thank

Regards

John,You are probably at the moment the guy who asks the most in these
John

You are probably at the moment the guy who asks the most in these
newsgroups, no problem, however by just copying in your problem in MSDN you
get all the answers you want about that

When you set your search string than use the trick to set as
well -blogs -forums to prevent that you get all kind of rubish

http://msdn2.microsoft.com/en-us/default.asp

When you can not get your answer there, or it is not clear to you, then
place your question here

Co

"John" <[email protected]> schreef in bericht

What are you trying to cut, copy, and paste?
What are you trying to cut, copy, and paste? The data displayed on the whol
form? Text in a textbox

RobinS
GoldMail.com

Re: Cut, Copy, Paste code
Text in fields

Thank

Regards

I only need code for actual Copy, Cut and paste operation.
I only need code for actual Copy, Cut and paste operation. I already hav
menus (infragistics) setup

Thank

Regards

Re: Cut, Copy, Paste code
"John" <[email protected]> schrie

I wonder why you want to do something that you don't know how to do it.
I want to build an aircraft, but I don't know how to do, so I don't do
it. If I work in an aircraft building company and am instructed to build
an aircraft and don't know how to do it, I'd wonder if it is the right
job for me. You seem to work in such a company and ask the fellow
workers to do the work for you because you don't know how to do it - so,
what do you get paid for

Armin

Add a context menu to the text controlAdd menu items i.e. copy/cut/paste.
Add a context menu to the text contro
Add menu items i.e. copy/cut/paste
Add commands to the menu item

Delet
My.Computer.Clipboard.SetText(TextBox1.SelectedText
TextBox1.Text = TextBox1.Text.Replace(TextBox1.SelectedText, ""

SelectAl
TextBox1.SelectAll(

Past
TextBox1.Text = My.Computer.Clipboard.GetTex

Und
TextBox1.Undo(

Cu
My.Computer.Clipboard.SetText(TextBox1.Text
TextBox1.Text = "

Also make sure to enable/disable menu items kind like the followin
Private Sub TextBox1_TextChanged(ByVal sender As System.Object
ByVal e As System.EventArgs
Handles TextBox1.TextChange

PasteToolStripMenuItem.Enabled = My.Computer.Clipboard.ContainsTex
DeleteToolStripMenuItem.Enabled = TextBox1.SelectionLength >
CutToolStripMenuItem.Enabled = DeleteToolStripMenuItem.Enable
CopyToolStripMenuItem.Enabled = DeleteToolStripMenuItem.Enabled
UndoToolStripMenuItem.Enabled = TextBox1.CanUndo
End Sub

Your context menu will replace the normal menu say on a textbox.

Hope this helps,
Kevin


Re: Cut, Copy, Paste code
Hi Kevin

Many thanks. This is very useful.

Regards


Re: Cut, Copy, Paste code
John wrote:

Before you go implementing all of that, have you right clicked on a text box
before? There is already a context menu with all of that in there. Just
wondering why you are replacing a working wheel...

Steve this is fine but I need to implement these in the main menu on the top.
Steve this is fine but I need to implement these in the main menu on the
top.

Thanks

Regards

Re: Cut, Copy, Paste code

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.

Re: Cut, Copy, Paste code
message

I know I'm late to this party but I have to comment on the some of these
solutions. Note that these are for Framework 2.0, but so are the original
solutions (or they wouldn't work with the My namespace).

Delete should NOT place the contents of the text box on the clipboard. It is
absolutely NOT standard Windows behavior to perform any type of clipboard
activity during a delete. So get rid of the first line. Next, the solution
to remove the text could give undesired side effects if the selected text
exists elsewhere in the Text property besides the selected portion. It would
be better to do this:

TextBox1.SelectedText = String.Empty

For Cut/Copy/Paste you can simply use the Cut(), Copy(), and Paste()
methods. The Paste solution shown above is wrong anyway, as it will replace
the ENTIRE contents of the text box, not just the selected portion as per
normal behavior. Same with Cut above; it's not using the selected text but
rather all the text.

Re: Cut, Copy, Paste code


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 =3D Me.ActiveControl

Do
If TypeOf aControl Is TextBox Then Exit Do
' Cast to ContainerControl to access next level
ActiveControl
cControl =3D CType(aControl, ContainerControl)
aControl =3D cControl.ActiveControl
Loop

If TypeOf aControl Is TextBox Then
GetControl =3D aControl
Else
GetControl =3D 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 =3D 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 =3D 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 =3D CType(GetControl(), TextBox)
If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text)
=3D 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) =3D
DialogResult.No Then
' Move selection to the point after the current
selection and paste.
tControl.SelectionStart =3D 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 =3D CType(GetControl(), TextBox)
' Determine if last operation can be undone in text box.
If tControl.CanUndo =3D 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


Submitted via EggHeadCafe - Software Developer Portal of Choice
Upload Photos to the Web with your SmartPhone
http://www.eggheadcafe.com/tutorial...6d-9227152fa0e7/upload-photos-to-the-web.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top