Tuesday, 15 February 2011

c# - Copy/Paste in Windows Forms with custom controls -


I am writing a small application in C #. I want to let my users copy and paste data around the application and also have some custom controls, for example, there is a color picker.

There is a copy of some default controls (at least text box) and paste the functionality already in the same way I want to do the same thing with my color picker, and at the top to copy and paste Want an 'edit' menu.

At the moment, I can not see how to do this Ctrl + c and Ctrl + V is my best way to capture commands and menu clicks and go through a function that uses some calls to search for focused control and then control the data or to control ( With a large scale if based on the type of statement centered control) copy No returns.

Optional every custom control starts writing important handling, but by this method I am not sure how to include the editing menu function.

How can I do this in an elegant or more 'standard' way?

The easiest way is to activate keypreview in the form and after that Follow the logic in the keydown event.

But another way might be useful:
If you have your win app a menu (for example, edit & copy> (copy)> copy (paste)).

Enable keyboard shortcut menu for them

  // // editToolStripMenuItem // this.editToolStripMenuItem.DropDownItems.AddRange (New System.Windows.Forms.ToolStripItem [] { This.copyToolStripMenuItem, this.pasteToolStripMenuItem}); This.editToolStripMenuItem.Text = "edit"; // // copyToolStripMenuItem // ** this.copyToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys) ((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.C))); ** this.copyToolStripMenuItem.Text = "& amp; Copy"; // // pasteToolStripMenuItem // ** this.pasteToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys) ((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.V))); ** this.pasteToolStripMenuItem.Text = "Paste & Paste";  

So you have your shortcuts to copy paste. Now just manage your menu clicks

  Private Zero copyToolStripMenuItem_Click (Object Sender, EventArgs e) {Image myData = this.ActiveControl.BackgroundImage; Clipboard.SetImage (MyData); } Private Zero pasteToolStripMenuItem_Click (Object Sender, EventArgs E) {Image myData = Clipboard. GetImage (); This.ActiveControl.BackgroundImage = myData; }  

Of course, you can make your menu invisible, if you do not want it to be shown to the user.

=========================================== ==================================== UPDATE for many controls < / P>

Code:

  Private Zero CopyToolStripMenuItem_Click (Object Sender, EventArgs E) {ICopyPasteable Control = ICopyPasteable as Sender; If (control! = Null) {control.CopyToClipboard (); }} Private Zero pasteToolStripMenuItem_Click (Object Sender, EventArgs E) {ICopyPasteable Control = ICopyPasteable as Sender; If (control! = Zero) {control.PasteFromClipboard (); }}} Public Interface IcopyPasteable {Zero CopyToClipboard (); Zero pasteframeclipboard (); } Public class mytextbox: textbox, iCapPableable {#region ICopyPasteable Membres Public Zero CopyToClipboard () {clipboard .set text (this.Text); } Public Zero Paste Frameclipboard () {If (Clipboard Content Content) {This.Text = Clipboard.GetText (); }} #endrian}  

No comments:

Post a Comment