473,411 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Cut, Copy and Paste?

Hi,

I've got a vb.net winforms app. Out of the box, I can use Ctrl X, C and V as
expected in controls like textboxes. I've got a menustrip, and if I click the
link "Add standard items" which adds Cut copy and paste commands, this
functionality no longer works. It seems now I need to implement handlers for
these functions. I tried to do so, but say the paste handler, I need to call
paste on a specific control and that isn't necessarily where the insertion
point is (could be a completely different control). Needless to say, this
functinoality works much better without these menu items. What's going on
here? Are users supposed to implement their own cut copy paste functions? If
so, how can I make them work regardless of which control I'm working in at
the time?

Thanks...

-Ben
Jul 20 '06 #1
6 9601

"Ben R." <be**@newsgroup.nospamwrote in message
news:47**********************************@microsof t.com...
Hi,

I've got a vb.net winforms app. Out of the box, I can use Ctrl X, C and V
as
expected in controls like textboxes. I've got a menustrip, and if I click
the
link "Add standard items" which adds Cut copy and paste commands, this
functionality no longer works. It seems now I need to implement handlers
for
these functions. I tried to do so, but say the paste handler, I need to
call
paste on a specific control and that isn't necessarily where the insertion
point is (could be a completely different control). Needless to say, this
functinoality works much better without these menu items. What's going on
here? Are users supposed to implement their own cut copy paste functions?
If
so, how can I make them work regardless of which control I'm working in at
the time?
I'm not sure I know what you mean by making "them work regardless of which
control I'm working in at the time."

The Paste operation is something you want to make universal to all forms and
all text-like objects on the forms. Normal operation is to insert the
contents of the Clipboard into whatever form and whatever object on the form
has focus at the point where the cursor is located (or the stuff that is
selected).

Determining the active form and object can be done from the parent using
Me.ActiveForm.ActiveObject.

The Clipboard.GetText (et al) will retrieve the contents of the Clipboard.

So from the parent you could do something as simple as
Me.ActiveForm.ActiveObject = Clipboard.GetText. If the Clipboard contains a
mixed bag of goods (text with embedded OLE objects, for example) you'll have
to work on a more involved solution.
Jul 20 '06 #2
Hi,

What is the solution that is employed by default when I highlight text and
press control + c and control + v? This works perfectly. Then, it stops
working once I add copy and paste menu items. What is the code that is
executed by default in the former scenario? Can I bring that to the scenario
where I have menu items for cut copy and paste?

Thanks...

-Ben

"MyndPhlyp" wrote:
>
"Ben R." <be**@newsgroup.nospamwrote in message
news:47**********************************@microsof t.com...
Hi,

I've got a vb.net winforms app. Out of the box, I can use Ctrl X, C and V
as
expected in controls like textboxes. I've got a menustrip, and if I click
the
link "Add standard items" which adds Cut copy and paste commands, this
functionality no longer works. It seems now I need to implement handlers
for
these functions. I tried to do so, but say the paste handler, I need to
call
paste on a specific control and that isn't necessarily where the insertion
point is (could be a completely different control). Needless to say, this
functinoality works much better without these menu items. What's going on
here? Are users supposed to implement their own cut copy paste functions?
If
so, how can I make them work regardless of which control I'm working in at
the time?

I'm not sure I know what you mean by making "them work regardless of which
control I'm working in at the time."

The Paste operation is something you want to make universal to all forms and
all text-like objects on the forms. Normal operation is to insert the
contents of the Clipboard into whatever form and whatever object on the form
has focus at the point where the cursor is located (or the stuff that is
selected).

Determining the active form and object can be done from the parent using
Me.ActiveForm.ActiveObject.

The Clipboard.GetText (et al) will retrieve the contents of the Clipboard.

So from the parent you could do something as simple as
Me.ActiveForm.ActiveObject = Clipboard.GetText. If the Clipboard contains a
mixed bag of goods (text with embedded OLE objects, for example) you'll have
to work on a more involved solution.
Jul 20 '06 #3
Hi Ben,

Thanks for your post!

Yes, I can reproduce this behavior. Let me clarify the internal work and
design to you:

1. The default Ctrl+C/V copy/paste behavior of TextBox is not implemented
by .Net, it is totally supported by underlying Win32 Edit control.(Note:
Net TextBox control is a wrapper around Win32 Edit control). If you open
Notepad, the editing rectangle in it is a big Edit control(you may use
Spy++ to verify this), you will find that the Edit control of notepad will
have Ctrl+C/V copy/paste function defaultly.

2. After you adding standard menustrip to the Form, this behavior is lost.
It is prohibited by ToolStripMenuItem.ShortcutKeys property. You will find
that the following code in Form1.Designer.vb file(note: in hidden text):
Me.CopyToolStripMenuItem.ShortcutKeys =
CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.C),
System.Windows.Forms.Keys)

By using this code, .Net winform will intercept all the Ctrl+C key
combination and translate it into CopyToolStripMenuItem.Click event. If you
are curious, below is the source code that intercepts the short cut key and
fires the Click event:

Protected Friend Overrides Function ProcessCmdKey(ByRef m As Message, ByVal
keyData As Keys) As Boolean
If ((Me.Enabled AndAlso (Me.ShortcutKeys = keyData)) AndAlso Not
Me.HasDropDownItems) Then
MyBase.FireEvent(ToolStripItemEventType.Click)
Return True
End If
Return MyBase.ProcessCmdKey(m, keyData)
End Function

As you can see, this function does not call MyBase.ProcessCmdKey if the
current user pressed the short cut key combination, so the underlying Edit
control code will not have chance to execute the default copy function.

So this behavior is by the design of ToolStripMenuItem.ShortcutKeys
property, although it looks strange.

Ok, we now know why the behavior is generated. I can provide a workaround
for this issue: simulate the underlying Edit control code in .Net.

The underlying Edit control actually monitors Ctrl+C key combination and
send a WM_COPY message to the Edit control to implement the default
function, so we can p/invoke SendMessage API and simulate this either.
Below is the sample code snippet for simulating the paste function:

Imports System.Runtime.InteropServices
Public Class Form1

<DllImport("user32.dll", CharSet:=CharSet.Auto)_
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As
Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
End Function

Private Const WM_PASTE As Integer = &H302
Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
SendMessage(Me.ActiveControl.Handle, WM_PASTE, 0, 0)
End Sub
End Class

You can also handle CopyToolStripMenuItem, CutToolStripMenuItem's Click
event handler to send WM_COPY, WM_CUT messages.

Hope this helps! If you still have anything unclear, please feel free to
tell me, thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 21 '06 #4
Hi Jeffrey,

Thanks for the response. Very thorough and helpful! I tried employing your
solution, and encountered some difficulty. Then I tried on a simple winforms
app with just a textbox and it worked properly. I think I figured out the
problem I'm now facing:

I set a breakpoint at:

SendMessage(Me.ActiveControl.Handle, WM_PASTE, 0, 0)

And examined what Me.ActiveControl is pointing to. The result:

{System.Windows.Forms.ToolStripContainer}

I would expect it to point to the textbox but that's not happening. I guess
it's not pointing deep enough in the control hierarchy. Do you know any way
to point deeper in the control hierarchy?

Thanks again for your help, Jeffrey...

-Ben

""Jeffrey Tan[MSFT]"" wrote:
Hi Ben,

Thanks for your post!

Yes, I can reproduce this behavior. Let me clarify the internal work and
design to you:

1. The default Ctrl+C/V copy/paste behavior of TextBox is not implemented
by .Net, it is totally supported by underlying Win32 Edit control.(Note:
.Net TextBox control is a wrapper around Win32 Edit control). If you open
Notepad, the editing rectangle in it is a big Edit control(you may use
Spy++ to verify this), you will find that the Edit control of notepad will
have Ctrl+C/V copy/paste function defaultly.

2. After you adding standard menustrip to the Form, this behavior is lost.
It is prohibited by ToolStripMenuItem.ShortcutKeys property. You will find
that the following code in Form1.Designer.vb file(note: in hidden text):
Me.CopyToolStripMenuItem.ShortcutKeys =
CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.C),
System.Windows.Forms.Keys)

By using this code, .Net winform will intercept all the Ctrl+C key
combination and translate it into CopyToolStripMenuItem.Click event. If you
are curious, below is the source code that intercepts the short cut key and
fires the Click event:

Protected Friend Overrides Function ProcessCmdKey(ByRef m As Message, ByVal
keyData As Keys) As Boolean
If ((Me.Enabled AndAlso (Me.ShortcutKeys = keyData)) AndAlso Not
Me.HasDropDownItems) Then
MyBase.FireEvent(ToolStripItemEventType.Click)
Return True
End If
Return MyBase.ProcessCmdKey(m, keyData)
End Function

As you can see, this function does not call MyBase.ProcessCmdKey if the
current user pressed the short cut key combination, so the underlying Edit
control code will not have chance to execute the default copy function.

So this behavior is by the design of ToolStripMenuItem.ShortcutKeys
property, although it looks strange.

Ok, we now know why the behavior is generated. I can provide a workaround
for this issue: simulate the underlying Edit control code in .Net.

The underlying Edit control actually monitors Ctrl+C key combination and
send a WM_COPY message to the Edit control to implement the default
function, so we can p/invoke SendMessage API and simulate this either.
Below is the sample code snippet for simulating the paste function:

Imports System.Runtime.InteropServices
Public Class Form1

<DllImport("user32.dll", CharSet:=CharSet.Auto)_
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As
Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
End Function

Private Const WM_PASTE As Integer = &H302
Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
SendMessage(Me.ActiveControl.Handle, WM_PASTE, 0, 0)
End Sub
End Class

You can also handle CopyToolStripMenuItem, CutToolStripMenuItem's Click
event handler to send WM_COPY, WM_CUT messages.

Hope this helps! If you still have anything unclear, please feel free to
tell me, thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 21 '06 #5
Hi Jeffrey,

I think my thought was correct and I've found a solution that works:

Private Sub CopyToolStripMenuItem1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CopyToolStripMenuItem1.Click,
CopyToolStripMenuItem.Click
Dim thecontrol As Control
thecontrol = Me.ActiveControl

Dim mytsc As ToolStripContainer = TryCast(thecontrol,
ToolStripContainer)

If mytsc IsNot Nothing Then
SendMessage(mytsc.ActiveControl.Handle, WM_COPY, 0, 0)
Else
SendMessage(Me.ActiveControl.Handle, WM_COPY, 0, 0)
End If
End Sub

It only goes one level deep, but does the job. I was thinking of using a
loop that would keep going deeper until it couldn't grab an "activecontrol"
property, but at least in my app, this seems to work. Thanks again for your
help!

-Ben

"Ben R." wrote:
Hi Jeffrey,

Thanks for the response. Very thorough and helpful! I tried employing your
solution, and encountered some difficulty. Then I tried on a simple winforms
app with just a textbox and it worked properly. I think I figured out the
problem I'm now facing:

I set a breakpoint at:

SendMessage(Me.ActiveControl.Handle, WM_PASTE, 0, 0)

And examined what Me.ActiveControl is pointing to. The result:

{System.Windows.Forms.ToolStripContainer}

I would expect it to point to the textbox but that's not happening. I guess
it's not pointing deep enough in the control hierarchy. Do you know any way
to point deeper in the control hierarchy?

Thanks again for your help, Jeffrey...

-Ben

""Jeffrey Tan[MSFT]"" wrote:
Hi Ben,

Thanks for your post!

Yes, I can reproduce this behavior. Let me clarify the internal work and
design to you:

1. The default Ctrl+C/V copy/paste behavior of TextBox is not implemented
by .Net, it is totally supported by underlying Win32 Edit control.(Note:
.Net TextBox control is a wrapper around Win32 Edit control). If you open
Notepad, the editing rectangle in it is a big Edit control(you may use
Spy++ to verify this), you will find that the Edit control of notepad will
have Ctrl+C/V copy/paste function defaultly.

2. After you adding standard menustrip to the Form, this behavior is lost.
It is prohibited by ToolStripMenuItem.ShortcutKeys property. You will find
that the following code in Form1.Designer.vb file(note: in hidden text):
Me.CopyToolStripMenuItem.ShortcutKeys =
CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.C),
System.Windows.Forms.Keys)

By using this code, .Net winform will intercept all the Ctrl+C key
combination and translate it into CopyToolStripMenuItem.Click event. If you
are curious, below is the source code that intercepts the short cut key and
fires the Click event:

Protected Friend Overrides Function ProcessCmdKey(ByRef m As Message, ByVal
keyData As Keys) As Boolean
If ((Me.Enabled AndAlso (Me.ShortcutKeys = keyData)) AndAlso Not
Me.HasDropDownItems) Then
MyBase.FireEvent(ToolStripItemEventType.Click)
Return True
End If
Return MyBase.ProcessCmdKey(m, keyData)
End Function

As you can see, this function does not call MyBase.ProcessCmdKey if the
current user pressed the short cut key combination, so the underlying Edit
control code will not have chance to execute the default copy function.

So this behavior is by the design of ToolStripMenuItem.ShortcutKeys
property, although it looks strange.

Ok, we now know why the behavior is generated. I can provide a workaround
for this issue: simulate the underlying Edit control code in .Net.

The underlying Edit control actually monitors Ctrl+C key combination and
send a WM_COPY message to the Edit control to implement the default
function, so we can p/invoke SendMessage API and simulate this either.
Below is the sample code snippet for simulating the paste function:

Imports System.Runtime.InteropServices
Public Class Form1

<DllImport("user32.dll", CharSet:=CharSet.Auto)_
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As
Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
End Function

Private Const WM_PASTE As Integer = &H302
Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
SendMessage(Me.ActiveControl.Handle, WM_PASTE, 0, 0)
End Sub
End Class

You can also handle CopyToolStripMenuItem, CutToolStripMenuItem's Click
event handler to send WM_COPY, WM_CUT messages.

Hope this helps! If you still have anything unclear, please feel free to
tell me, thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Jul 21 '06 #6
Hi Ben,

Cool, you resolved this further issue before I reviewed it! You will
benifit more from figuring out yourself than learning from others :-)

Additionally, if you know of your control hierarchy accurately, your code
snippet will just work without any problem. To develop a more general
solution, you may just loop through the control hierarchy and find the
deepest active control, like this:

<DllImport("user32.dll", CharSet:=CharSet.Auto)_
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As
Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
End Function

Private Const WM_PASTE As Integer = &H302
Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
Dim cc As ContainerControl = Me
Dim ac As Control
Do
ac = cc.ActiveControl
Try
cc = CType(ac, ContainerControl)
Catch ex As Exception 'if the ActiveControl is not a
ContainerControl, an exception will throw
Exit Do 'We have found the
inner most control, so exit loop
End Try

Loop
SendMessage(ac.Handle, WM_PASTE, 0, 0)
End Sub

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Legendary Pansy | last post by:
I was checking out the 101 C# Samples, specifically Windows Forms - Use the Clipboard. I took a look at the code for a while, and I understand what the program is doing with the cut, copy, pasting...
3
by: Rachel Suddeth | last post by:
This may not be the right forum, but it's a problem I chiefly come across when trying to post here. When I do a copy/paste from VS, the text always looks really weird (and even if I'm in an...
2
by: Matt | last post by:
Hello, I have a copy button and a paste button. What code should I add to the copy button and the paste button to do it's work? Thanks, Matt
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
0
by: jshoffner | last post by:
This sounds like a really silly question but I'm can't find a solution anywhere. I use the Search window within VS.NET 2003 all the time. However, when I find a sample that is usefull I would like...
5
by: Kaur | last post by:
Hi, I have been successful copying a vba code from one of your posts on how to copy and paste a record by declaring the desired fields that needs to be copied in form's declaration and creating two...
17
by: Steve | last post by:
I'm trying to code cut, copy, and paste in vb 2005 so that when the user clicks on a toolbar button, the cut/copy/paste will work with whatever textbox the cursor is current located in (I have...
5
by: phill86 | last post by:
Hi I have a main form that holds records for scheduled meetings, date time location etc... in that form i have a sub form that has a list of equipment resources that you can assign to the meeting in...
8
by: jh | last post by:
I'd like to copy/paste into a listbox during runtime. I can do this for a textbox but can't figure out how to accomplish this for a listbox. Any help? Thanks.
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.