By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,930 Members | 1,487 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,930 IT Pros & Developers. It's quick & easy.

Cut, Copy, Paste code

P: n/a
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
Mar 20 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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

"John" <in**@nospam.infovis.co.ukschreef in bericht
news:Oq**************@TK2MSFTNGP06.phx.gbl...
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
Mar 20 '08 #2

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

RobinS.
GoldMail.com

"John" <in**@nospam.infovis.co.ukwrote in message
news:Oq**************@TK2MSFTNGP06.phx.gbl...
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
Mar 20 '08 #3

P: n/a
On Mar 20, 4:22 am, "John" <i...@nospam.infovis.co.ukwrote:
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.
Mar 20 '08 #4

P: n/a
Text in fields.

Thanks

Regards

"RobinS" <ro****@imnottelling.comwrote in message
news:hI******************************@comcast.com. ..
What are you trying to cut, copy, and paste? The data displayed on the
whole form? Text in a textbox?

RobinS.
GoldMail.com

"John" <in**@nospam.infovis.co.ukwrote in message
news:Oq**************@TK2MSFTNGP06.phx.gbl...
>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

Mar 20 '08 #5

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

Thanks

Regards

"kimiraikkonen" <ki*************@gmail.comwrote in message
news:e5**********************************@a23g2000 hsc.googlegroups.com...
On Mar 20, 4:22 am, "John" <i...@nospam.infovis.co.ukwrote:
>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.

Mar 20 '08 #6

P: n/a
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.SelectedTex t)
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

"John" <in**@nospam.infovis.co.ukwrote in message
news:Oq**************@TK2MSFTNGP06.phx.gbl...
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

Mar 20 '08 #7

P: n/a
Hi Kevin

Many thanks. This is very useful.

Regards

"ke*************@state.or.us" <Ke***************@state.or.uswrote in
message news:%2****************@TK2MSFTNGP06.phx.gbl...
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.SelectedTex t)
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

"John" <in**@nospam.infovis.co.ukwrote in message
news:Oq**************@TK2MSFTNGP06.phx.gbl...
>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


Mar 20 '08 #8

P: n/a
"John" <in**@nospam.infovis.co.ukschrieb
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

Mar 20 '08 #9

P: n/a
John wrote:
Hi Kevin

Many thanks. This is very useful.

Regards
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...
Mar 20 '08 #10

P: n/a
Steve this is fine but I need to implement these in the main menu on the
top.

Thanks

Regards

"Steve Gerrard" <my********@comcast.netwrote in message
news:Vq******************************@comcast.com. ..
John wrote:
>Hi Kevin

Many thanks. This is very useful.

Regards

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


Mar 20 '08 #11

P: n/a
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
Jun 27 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.