Question

We have a standard textbox in a Winforms application which responds to paste (both right-click menu and CTRL+V) in the normal way (ie it pastes) in our dev environment.

At one customer site the paste is mostly completely ignored (behaves as if there's nothing in the clipboard). We've tested it with both single line and multiline versions of the TextBox and we've created a standalone app with just a couple of TextBoxes and on this one client site the problem persists. Pasting mostly doesn't work.

In further testing we find that simply asking for the clipboard's content within a test winforms app, it comes back as empty string. Double checking with Notepad we find there's definitely something in the clipboard.

Here's what we've checked:

  • In tests we ensure the source of the clipboard is from Notepad or indeed within the textbox itself so we know it isn't something freaky from HTML/Word
  • We can always type into the textbox, so it isn't as if the textbox isn't allowing modifications
  • Quantity of text we've tried it with large and small amounts of text in the clipboard, makes no difference
  • Right-click paste versus CTRL+V: they either both work or neither work - so all those posts out there which are about fixing either one or the other are no help to us
  • Looking for patterns I think that once it's failed once it doesn't work again until the app is restarted but I'm not sure
  • When the paste problem does occur, cut & copy are unaffected and continue to work
  • Client's machines paste function definitely works on their other apps, Notepad, Office etc

Remember that the very same compiled application always pastes successfully on our dev machines and does occasionally paste successfully on the customer's machines! This is what makes it so mysterious.

In all cases we've verified that there is something in the clipboard by pasting into Notepad alongside our app.

Has anyone else has seen this and/or can suggest an explanation?

Update/Further investigation
Could it be that it's something to do with threading? We don't do anything interesting with threading and we've never had to worry about using an STAThread attribute. But the MSDN page says:

The Clipboard class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.

So, in a Winforms project with no main thread - just a startup form, where do you put this attribute? And why don't we need it on the dev machines? And how come we've never needed to use it on any of countless other Winforms apps we've made?

Was it helpful?

Solution 2

I have a fancy firewall that is also able block applications from looking at data in the clipboard, on per-application basis.

It does not prevent writing; the point of the feature is to stop malware from stealing important information that might end up in the clipboad, such as a password.
It can also block any given application from doing various other system tasks, e.g. taking a screenshot or navigating to a web page with the default system browser.

The customer might have something like that installed, with Notepad allowed in the rules.

OTHER TIPS

You are giving very little to go by. Pretty unlikely that this is caused by a problem in your program, far more likely that this is an environmental problem that's specific to the user's machine.

Some background. The TextBox control in a Winforms app is the exact same component that Notepad uses to allow you to edit text. The underlying native component is an Edit control, it has been a standard component in Windows since version 1.0. Note how the context menu you get when you right-click the TextBox and Notepad is identical. There is no distinction between the Paste command in that menu and pressing Ctrl+V, they both cause the WM_PASTE message to be sent to the native Edit control. Which internally deals with the clipboard, that code is entirely outside of your reach and operates identically in Notepad as it does in your program.

A thread apartment problem is unlikely, the customer ought to also have a problem with the Copy command and you should have noticed it before. In C#, it is set by the [STAThread] attribute on the Main() method, it is compiler-generated in VB.NET.

There are plenty of utilities that can cause the clipboard to misbehave. First and foremost are clipboard viewers, programs that hook into clipboard notifications. AddClipboardFormatListener() is the underlying winapi function. They tend to do things like "enhancing" the clipboard by allowing it to store more than one item or give another view of what is on the clipboard. They tend to destabilize the machine by not passing notifications on to the next viewer properly or by breaking the viewer chain by not unregistering themselves correctly. Such a broken chain in itself is a lead to "works okay in Notepad, not in mine".

Such kind of problems are very hard to diagnose of course and don't typically get resolved until the computer user thoroughly cleans his machine. That this is his problem and not yours is always difficult news to deliver, can't really help you with that.

Some times it it possible that other third party application is controlling your clipboard like Snagit it is possible that third party applications have their on filters for clipboard for standarad controls like notepad and other windows based application.

What you can do is you have to find out any other application on client's machine is having access on clipboard. You can check through task manager or running process. It may help you.

I have faced similar kind of issue with Snagit application. This application was preventing my program to setting clipboard text for their own use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top