I am doing C#/.NET app. I want to make a button on toolbar that will basically invoke Ctrl+C (copy to clipboard). I looked to Clipboard class, but problem is since I have multiple textboxes on form, I would need to scan which one has focus and if/is selected text, in order to select text from it etc., so I think there must me “one-liner” solution.
Any ideas?
(Also, how to add all 3: Cut, Copy, Paste to toolbar, under same conditions- multiple tekstboxes on main form..)
If you are using WinForms I possibly have a small Solution for that problem.
Create an Object to store your last selected TextBox
In your Constructor create an Eventhandler for each
TextBox
in yourForm
for theGotFocus
-Event by calling theAddGotFocusEventHandler
-Method with the parameterthis.Controls
.And set the
lastSelectedTextBox
to your currently selected TextBoxIn your Click-EventHandler for the button check if selectedText is null and copy the text to clipboard:
Edit: If for Winforms..
Place this in your invoke function:
As mentioned below by Daniel Abou Chleih: If you have to interact with a control to invoke the function the focus will be changed to that control. This only works if you call it through other means.
Edit: Not a one-liner but works on the last active TextBox:
Now you can call CopyActiveText() to get the most recent TextBox that lost focus last or currently has focus.