472,954 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 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 9538

"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, Im 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.
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.