Raising Events / Copy - Paste

I have a main menu in my form class with Edit->Copy and Edit->Paste. I assigned the normal shortcut keys of Ctrl+C and Ctrl+V to these menu items.

However, the Form that gets notified when these events fire can't really handle the events. I want the form's "ActiveControl" member to be notified of the event so that my individual windows that need copy/paste support can have their own code for talking to the Clipboard.

How can I notify these windows of the Copy/Paste operations

One of my first thoughts was to have them implement a delegate function defined in my main Form, but that isn't really too easy to get to the lowest level window that needs copy paste support.

I'd rather fire a KeyDown event to the ActiveControl so it can handle the Ctrl+C and Ctrl+V key down events themselves.

Is it possible to invoke a KeyDown event on a Control programatically  Or should I be going with a different notification method altogether

I've tried overriding KeyDown in my controls, but it never reaches them since the Shortcut/Accelerator key in the main menu is capturing it first and consuming it.

Thanks,
Aaron





Answer this question

Raising Events / Copy - Paste

  • timvw

    Not sure I understand you. What does that have to do with raising an event on a window programatically


    Maybe I didn't explain well enough.

    I have a grid control inside of a panel, inside of a panel, inside of a panel, inside of a tab page, inside of a tab control, inside of a panel, inside of the MainFrame.

    When the grid control has the focus, and I hit Ctrl+C, the OnKeyDown event never gets called in the Grid. This is because I setup the MainFrame's main menu to capture Ctrl+C as a shortcut key for Edit->Copy in the main menu.

    I have found that the Form.ActiveControl member variable is pointing to my grid control when the MainFrame handles the Ctrl+C operation. I am wondering if there is someway to raise a KeyDown event on the ActiveControl so I can have the grid control handle the copy operation.

    Maybe there is some other way to do this   My program generically sets up panels inside of Tab Pages and it would be difficult to give the specific panel that contains the grid control access to a delegate function in the MainFrame. That's why I'm looking for a way to raise an event on the ActiveControl. 

    Plus, this will work for me as a generic means to handle the copy / paste operations. Because then I can let my other windows such as TreeViews handle copy/paste in the same manner. They would be the ActiveControl when they have the focus, so they would get the KeyDown event at that time.

    Basically, by using a Shortcut key on the main menu, it has prevented all of my windows from being able to capture Ctrl+C as a KeyDown event. I am looking for a generic way to notify any ActiveControl that they should handle the operation.

    Thanks,
    Aaron

  • cablehead

    Apologies. I thought you just wanted to be able to tell when something was pasted into a control since pasting doesn't always trigger the key events. Sounds now like you're talking event bubbling.
  • urpalshu

    No probs, I appreciate any help.

    Anyway, I think I have a solution. I will try it when I get to work tomorrow. Surprised I didn't think of it earlier, although my solution may be a hack if there is "event bubbling". Never heard of that before.

    I will just define an interface, IMyCopyPaste that has a Copy method and a Paste method. Then I will have any control that needs to support copy paste implement that interface. So when my MainFrame captures the Ctrl+C or Ctrl+V shortcuts, I can just attempt to cast the Form.ActiveControl member to IMyCopyPaste. If it casts, then voila I now have access to that control's functions for copy and paste. Gotta love interfaces!!!

    At first I thought of using reflection to see if the control had a known public method, then it dawned on me that I would definitely know it has public methods if they implement a known interface. Duh!!!

  • mmcqueen

    Try this. The Me.Visible line is to prevent the event from firing on load.    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            If Me.Visible Then
                MessageBox.Show("I'm changed forever!")
            End If
        End Sub

  • white_angel_22

    I know, event bubbling sounds wierd but it's a real thing. It means "bubbling" the events from a sub control to an upper level control and it sounded like what you wanted. I've read about it so I knew about but I didn't have any code examples for you.

    Regarding whether it exists or not . . . I'm not sure. A strict search on Google only returned a little more than 1,750 references to it. Here's a few.

    <a href="http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconeventbubblingcontrolsample.asp">MSDN</a>

    <a href="http://www.webreference.com/js/column10/eventbubbling.html">Webreference</a>

    <a href="http://www.wdvl.com/Authoring/JavaScript/Events/bubbling.html">Way back in 1999</a>

    <a href="http://www.aspalliance.com/hmcheung/Articles/030331/Default.aspx">ASP Alliance</a>


  • Raising Events / Copy - Paste