How to implement the Edit -> Copy menu in c#/.net -


how implement copy menu item in windows application written in c#/.net 2.0?

i want let user mark text in control , select copy menu item edit menu in menubar of application , paste in example excel.

what makes head spin how first determine child form active , how find control contains marked text should copied clipboard.

help, please.

with aid of heavy pair programming colleague of mine , came this, feel free refactor.

the code placed in main form. copytoolstripmenuitem_click method handles click event on copy menu item in edit menu.

    /// <summary>     /// recursively traverse tree of controls find control has focus, if     /// </summary>     /// <param name="c">the control search, might control container</param>     /// <returns>the control either has focus or contains control has focus</returns>     private control findfocus(control c)      {         foreach (control k in c.controls)         {             if (k.focused)             {                 return k;             }             else if (k.containsfocus)             {                 return findfocus(k);             }         }          return null;     }      private void copytoolstripmenuitem_click(object sender, eventargs e)     {         form f = this.activemdichild;          // find control has focus         control focusedcontrol = findfocus(f.activecontrol);          // see if focusedcontrol of type can select text/data         if (focusedcontrol textbox)         {             textbox tb = focusedcontrol textbox;             clipboard.setdataobject(tb.selectedtext);         }         else if (focusedcontrol datagridview)         {             datagridview dgv = focusedcontrol  datagridview;             clipboard.setdataobject(dgv.getclipboardcontent());         }         else if (...more?...)         {         }     } 

Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -