views:

373

answers:

4

I am writing a small app in C# using windows forms. I want to let my users copy and paste data around the app and there are some custom controls, for example one is a colour picker.

Some of the default controls (well at least the TextBox) have a copy and paste functionality already. I want to have the same thing with my colour picker, and also want an 'Edit' menu at the top to copy and paste.

At the moment I can't see how to do this in a nice way, my current tack is to catch the Ctrl-C and Ctrl-V commands and the menu clicks and go through a function which uses some Win32 to find the focused control and then copy or paste data from or to the control (with a massive if statement depending on the type of the focused control).

The alternative seems to be to write key handling into every custom control, but with this method I'm not sure how to incorporate the Edit menu functions.

Does anyone have any suggestions on how to do this in an elegant or more 'standard' way?

A: 

Hai ,

Have look at this It is similar to ur prob just check it out..

Pandiya Chendur
Thanks, this mostly seems to be talking about actually using the Clipboard, which I have no problem with. I didn't know about TextBox.Copy() but what I'm trying to get is a better way to wire in copy and paste to the custom controls, so they respond to keyboard and menu clicks but without a lot of if statements or per-control key handling code.
oh_cripes
A: 

to find the focussed control: ContainerControl.ActiveControl Then depeding on the type of control you can set a value (with the clipboard value)

PoweRoy
Thanks, I already am able to get the focused control, my question is about how I should organise my app. It is pretty ugly to have to do a big 'if else' on every type of control, and it also means I have to have a case for things like TextBoxes, which already have Clipboard management in them.
oh_cripes
Depends what you understand on ugly, 1 function with a switch/ifelse or multiple functions for multiple controls that do the same thing. I would prefer 1 function to do all the cases. If you copy a color, what would a textbox do when it receives a paste? hex value for color or something different. I suggest you make a schematic for yourself of what controls should interact with each other. If you still don't know what the best strategy is, post the schematic here :)
PoweRoy
+1  A: 

Try this also cut-copy-paste-code

Pandiya Chendur
+2  A: 

The simplest way is to activate KeyPreview in the form and then follow the logic in KeyDown event.

But an other approach can be useful:
If you have in your win application a menu (by e.g. &Edit => Copy (Paste)).

Enable for that menus the keyboard shortcuts

// 
// 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 = "&Copy";
// 
// pasteToolStripMenuItem
// 
**this.pasteToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)
((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.V)));**
this.pasteToolStripMenuItem.Text = "&Paste";

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

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
    Image myData = this.ActiveControl.BackgroundImage;
    Clipboard.SetImage(myData);
}

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
    Image myData = Clipboard.GetImage();
    this.ActiveControl.BackgroundImage = myData;
}

Surely, you can make invisible your menu, if you want do not show it to the user.

=============================================================================== UPDATE

code for multiple controls:

    private void copyToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ICopyPasteable control = sender as ICopyPasteable;
        if (control != null)
        {
            control.CopyToClipboard();
        }
    }

    private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ICopyPasteable control = sender as ICopyPasteable;
        if (control != null)
        {
            control.PasteFromClipboard();
        }
    }
}


public interface ICopyPasteable
{
    void CopyToClipboard();
    void PasteFromClipboard();
}

public class MyTextBox : TextBox, ICopyPasteable
{

    #region ICopyPasteable Membres

    public void CopyToClipboard()
    {
        Clipboard.SetText(this.Text);
    }

    public void PasteFromClipboard()
    {
        if (Clipboard.ContainsText())
        {
            this.Text = Clipboard.GetText();
        }
    }

    #endregion
}
serhio
Thanks for your response. The trouble I have is that I want to be able to copy and paste between different controls. So in my pasteToolStripMenuItem_Click() method for example I have to get the focused control and then go through a big if statement depending on what type of control it is (and also what type of data is in the clipboard) so that I can deal with each case. I would really like to have each control deal with the copying and pasting itself. I tried making an abstract superclass to do this but it breaks the winforms designer.
oh_cripes
see my update code bellow the line. Instead of converting the sender you can convert the form activeControl to the ICopyPasteable interface.
serhio
I think this may be the way to go, cheers.
oh_cripes
Don't forget that people like me still use Ctrl-Insert for copy and Shift-Insert for paste.
Vincent McNabb