Setting the clipboard text sequentially

Setting the clipboard text sequentially

nbonnett-murphy
Advocate Advocate
706 Views
9 Replies
Message 1 of 10

Setting the clipboard text sequentially

nbonnett-murphy
Advocate
Advocate

Hello,

 

I'm trying to push text to the clipboard 2 times in a row. I know it's an unusual thing to do, but I like to use the clipboard history feature of windows (start+v). So as part of a larger "fast info" script, I want to push the part description onto the clipboard, then as a separate operation, push the part number.

 

Here is what I have now:

Imports System.Windows.Forms
Clipboard
.SetText(ITEMPropertySet.Item("Description").Value)
Clipboard.SetText(ITEMPropertySet.Item("Part Number").Value)

The problem is that it only shows the part number in the clipboard history, which makes sense, since I set it then immediately over write it.

 

If I go like this:

Imports System.Windows.Forms
Clipboard
.SetText(ITEMPropertySet.Item("Description").Value)
msgbox("this is a message box")
Clipboard.SetText(ITEMPropertySet.Item("Part Number").Value)

Then both properties show up in the history. I'm gathering from this that the message box causes some sort of "go" or "update" or "execute" operation to happen that commits the clipboard text setting. Is there a way that I can force this to happen?

 

I had high hopes for system.windows.forms.application.DoEvents(), but that doesn't seem to work as expected.

0 Likes
Accepted solutions (1)
707 Views
9 Replies
Replies (9)
Message 2 of 10

Frederick_Law
Mentor
Mentor

https://learn.microsoft.com/en-us/dotnet/api/system.windows.clipboard.settext?view=windowsdesktop-7....

Try use a different Clipboard: system.windows.clipboard

instead of: system.windows.forms.clipboard

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.clipboard.settext?view=windowsdesk...

This one clear clipboard before adding text

0 Likes
Message 3 of 10

nbonnett-murphy
Advocate
Advocate

I don't seem to have access to the system.windows.clipboard class, just the forms one. I get a "not a member" error if I leave out forms.

 

I think I see what you're saying though. By messing around with system.windows.forms.clipboard.clear and .settext, I'm gathering that it's the automatic clearing that's wiping out the data I want to see in the clipboard.

 

Is there some secret to getting at the system.windows namespace?

0 Likes
Message 4 of 10

Frederick_Law
Mentor
Mentor

I can't get to it in iLogic either.

0 Likes
Message 5 of 10

Frederick_Law
Mentor
Mentor

Found out how to add reference here:

https://github.com/koechlm/iLogic-Filter-Configurator/tree/master/WpfApp1

 

Add these to header:

AddReference "PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
AddReference "PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

Maybe only one of them is needed.

Somehow it made the rule "unsafe".

I did add text to clipboard.

0 Likes
Message 6 of 10

nbonnett-murphy
Advocate
Advocate

Eh, thanks for the help. It's not a critical thing and I spent too much time on it today anyways. I tried an ugly workaround by popping up the message box and then using sendkeys to hit the "enter" key, but I'm running into the same issue. The sendkeys doesn't fire until after the message box has been closed by the user.

 

I think there's some windows.forms message queueing situation that's above my head. Like I somehow need to open a thread, setText on the clipboard, close the thread, and then do that same operation again.

 

Edit: the AddReference you sent does give me access to that member of system.windows, but it still seems to be queuing commands in the same way.

0 Likes
Message 7 of 10

Frederick_Law
Mentor
Mentor

Interesting results:

Sub Main()
	System.Windows.Clipboard.SetText("abc")
	Logger.Info(System.Windows.Clipboard.GetText)
	System.Windows.Clipboard.Flush
	System.Windows.Clipboard.SetText("1234")
	Logger.Info(System.Windows.Clipboard.GetText)
	System.Windows.Clipboard.Flush
	System.Windows.Clipboard.SetText("abcde1234")
	Logger.Info(System.Windows.Clipboard.GetText)
	System.Windows.Clipboard.Flush
End Sub

 

They're in the clipboard but not in the clipboard 😵

0 Likes
Message 8 of 10

Frederick_Law
Mentor
Mentor
Accepted solution

Got it.

This works:

 

imports System.Windows.Forms
Sub Main()
	System.Windows.Forms.Clipboard.SetText("abc")
	System.Threading.Thread.CurrentThread.Sleep(250)
	System.Windows.Forms.Clipboard.SetText("1234")
	System.Threading.Thread.CurrentThread.Sleep(250)
	System.Windows.Forms.Clipboard.SetText("abcde1234")
	System.Threading.Thread.CurrentThread.Sleep(250)
End Sub

 

 

0 Likes
Message 9 of 10

nbonnett-murphy
Advocate
Advocate

Awesome thanks!

 

I also tried to use "getText" between the sets but had, I assume, the same results that you did. I'm surprised that sleep works and I'd love to find out what exactly the trigger is to "lock it in".

0 Likes
Message 10 of 10

Frederick_Law
Mentor
Mentor

Saw it while searching about clipboard.

Sleep(200) didn't work.  You can test and find the lowest number that works.

0 Likes