Copy to Clipboard in a GUI Application
- Details
- Written by June Blender
- Last Updated: 08 April 2016
- Created: 27 May 2015
- Hits: 26901
During #PSBlogWeek, I shared with my co-authors a little GUI-based blogging tool that gets links to the online version of PowerShell help topics, including About topics. I didn’t think much of it, because it was my first GUI app, but I soon received a nice compliment from PowerShell MVP Jeffery Hicks with a suggestion to add a “Copy to clipboard” button to the app. It was a great suggestion, so I set aside some time to implement it.
It didn’t take much time at all!
Using Clip.exe
I started by creating a little GUI app to isolate and test the copy-to-clipboard feature. When the user types text in the “Type here” text box and clicks Copy to clipboard,” the app copies the text in the textbox and saves it in the clipboard to be pasted elsewhere.
Implementation is pretty simple. When the text in the input textbox changes, I verify that it’s not an empty string and then enable the Copy to clipboard button.
$textboxInput_TextChanged={ if ($textboxInput.Text.Trim() -ne "") { $buttonCopyToClipboard.Enabled = $true } } |
For my first try at implementing the copy-to-clipboard feature, in the Click event of the Copy to Clipboard button, I got the Text property of the input textbox ($textboxInput.Text), trimmed it, and piped it to Clip.exe.
$buttonCopyToClipboard_Click={ $textboxInput.Text.Trim() | Clip } |
When I tested the app, it copied the text in the input text box to the clipboard as expected, but, in the process, it opened and closed a command prompt window. It was quick, but you could see the window flash and I wasn’t happy with the experience.
Using the Clipboard Class
Next, I tried the Copy to clipboard snippet in PowerShell Studio 2015. It invokes the SetText static method of the Clipboard class (System.Windows.Forms.Clipboard).
$buttonCopyToClipboard_Click={ [System.Windows.Forms.Clipboard]::SetText($textboxInput.Text.Trim()) } |
That worked perfectly! I could have stopped there, but I wanted to explore some other ways of copying to the clipboard.
Using the Copy Method
The Textbox class (System.Windows.Forms.Textbox) has a Copy method that copies the text in a Textbox to the clipboard. It’s very simple to read and to use, because it doesn’t have any parameters. Even better, Textbox controls actually “inherit” this method from their parent class, TextBoxBase, which means that the Copy method also works in other textbox controls with the same parent class, including RichTextbox and MaskedTextbox.
I was sold on the Copy() method … except for one problem.
$buttonCopyToClipboard_Click={ $textBoxInput.Copy() } |
It didn’t work. It didn’t generate any errors, but it didn’t copy the text in the input textbox to the clipboard.
To figure out why, I read the Copy method description really carefully and examined the examples in C#, C++, and VB, because they don’t have PowerShell examples on the .NET reference pages.
“Copies the current selection in the text box to the Clipboard.”
Aha! Selection. It copies the “selected” text in the textbox to the clipboard.
To select text from the Textbox, you use the Select or SelectAll methods of the Textbox. To keep it simple, I used SelectAll.
$buttonCopyToClipboard_Click={ $textboxInput.SelectAll() $textBoxInput.Copy() } |
Excellent. That’s another technique that I can use. Let’s see what lurks in the PowerShell 5.0 preview.
Using Set-Clipboard
The Windows PowerShell 5.0 preview includes new Get-Clipboard and Set-Clipboard cmdlets with cool parameters like Append, Format, and Raw. The Value parameter of Set-Clipboard takes value from the pipeline, so i just piped the trimmed text in the textbox to Set-Clipboard.
$buttonCopyToClipboard_Click={ $textBoxInput.Text.Trim() | Set-Clipboard } |
This worked perfectly. The only problem is that it requires PowerShell 5.0, which is brand new and still in preview. I want my app to run on most platforms.
Finishing Touches: StatusBar
After experimenting with the variety of Copy techniques in the Copy to Clipboard form, I added the SetText() method to my blogging app. It worked just fine, but clicking the Copy to clipboard button wasn’t very satisfying to me without some confirmation that the copy operation actually worked.
After the copy operation, I wanted to confirm that the text on the clipboard matched the text in the input textbox, and then let the user know about it. But, none of the copy or set methods returns an object, or even an error.
I used the ContainsText (is there anything on the clipboard?) and GetText (get the text on the clipboard) static methods of the Clipboard class. Because an –AND statement in Windows PowerShell doesn’t test the second condition if the first fails, I used the –AND to invoke both the ContainsText() and GetText methods.
if ([System.Windows.Forms.Clipboard]::ContainsText() –AND [System.Windows.Forms.Clipboard]::GetText() ... ) |
Then, I compared the Text in the textbox – the text that I wanted to copy — to the text that GetText() found on the clipboard.
if ([System.Windows.Forms.Clipboard]::ContainsText() –AND [System.Windows.Forms.Clipboard]::GetText() –eq $textboxInput.Text.Trim() ) ... |
Finally, I added a StatusBar to my form. If the copy succeeded, I set the Text in the StatusBar to “Copied to clipboard” and made the status bar visible. I didn’t want the StatusBar hanging around there all the time, so I used the Start-Sleep cmdlet to count 2 seconds, and then I changed the Visible property of the StatusBar to $false.
Here’s the Click event for the Copy to Clipboard button:
$buttonCopyToClipboard_Click={ $copyText = $textBoxInput.Text.Trim() [System.Windows.Forms.Clipboard]::SetText($copyText) if ([System.Windows.Forms.Clipboard]::ContainsText() -AND [System.Windows.Forms.Clipboard]::GetText() -eq $copyText) { $statusbar1.Text = "Copied to clipboard." $statusbar1.Visible = $true Start-Sleep -Seconds 2 $statusbar1.Visible = $false } } |
And, here’s a demo of it working in my GUI test app.
June Blender is a technology evangelist at SAPIEN Technologies, Inc. You can reach her at This email address is being protected from spambots. You need JavaScript enabled to view it. or follow her on Twitter at @juneb_get_help.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.