new Clipboard every time?

From a textfield I cut parts of the text and paste it at the end of it. Works.
Do I take an other part of it and paste it at the end again, the first clipboard content is being pasted!

At the beginning of the application: cb = new clipboard
In edit cut: cb.text = field.seltext
In edit paste: field.text = left(field.text,field.text.selstart) + cb.text + mid(field.text,field.selstart+1+field.sellength)

Example: text ‘aabbcc’, cutting out ‘bb’ and paste it at the end: ‘aaccbb’.
Then cutting ‘cc’ out and paste it at the end, it looks like that: ‘aabbbb’. Strange!
It works apparently only one time…

However, if I put the cb = new clipboard in the edit cut method, it works. I assumed the clipboard would be new and empty, but it isn’t…
I don’t get it: I have to create for every single clipboard action a new clipboard again!

A bug, or what am I missing here…?

2017 1.1. osx

Create a new clipboard object every time you need to do something with the clipboard. Don’t reuse them. The clipboard object is a snapshot of the clipboard; it gets stale quickly.

Yes, it seems that way. But why? Is that ment to be!!? Didn’t find anything in the documentation…

I would expect that cb.text should be overwritten by a new copy/cut, but from your experience the data is being appended. A workaround would be to clear cb.text (cb.text = “”).

To my mind this would be a bug, but not one I’ve encountered because I’ve always created a new clipboard as needed.

It’s been a while, but I though the documentation was pretty clear on this point. I’ll have to go back and look.

Yes. I didn’t find that in the documentation, but that’s the way it’s designed to be. It would be good if the docs clarified that point.

The docs say:

[quote]Dim text1 As String = “This is the first item being put on the clipboard!”
Dim text2 As String = “This is the second item being put on the clipboard”
Dim c As New Clipboard
c.Text = text1
c.Text = text2 // Overwrites first text
So setting Text overwrites, therefore I would say you must have an error in your code.

And you have not posted the real code, as your code does not compile:

field.text = left(field.text,field.text.selstart) + cb.text + mid(field.text,field.selstart+1+field.sellength) // field.text.selstart does not compile, replace it with field.selstart and everything is ok

You are right: this is how it is meant to be.

How can you get the Clipboard Contents if each time you create an instance, its contents is cleared ?

Thanks for your replies.
@ Eli: the code wasn’t meant to be compiled, but to demonstrate. My real code uses longer variables names, and I cut it shorter. And yes, not field.text.selstart
@ Emile: I was referring to new cb = new Clipboard, then it would be new and empty, no?

So, according the documentation its is just like a ‘universal’ variable string, where you just put text in it and from it. But, that’s my point, in my application, when using it as in the documentation c.Text = text2, it is not replaced, containing still text1 (because it is already ‘occupied’?)! Unless, the code goes every time thru another cb = new Clipboard command in my menu edit cut or copy method for a second copying of text out of the textfield.

So, is it just me (my coding, using just once a ‘cb = new clipboard’ instead of in every method I access the clipboard - this how it works well, now), or a bug, and if not, not well documented?

E.g. with Date: one dt = new Date is sufficient at the beginning the application to work with that date property in all methods.
Correct me if I’m wrong with all of that, please!


I already gave the reason why, but…

If it does what you believe, how can you copy something from an application and paste it somewhere since there is only one command to deal with the Clipboard.

Remember, you have a way to know if there is some text in the Clipboard:

[code]Dim Clip As New Clipboard

If Clip.TextAvailable Then[/code]

Also, you can do:

[code]Dim Clip As New Clipboard

Clip.Text= “”[/code]

to clear the previous Clipboard contents. Wayne already wrote that.


Dim Clip As New Clipboard

creates a new instance of the Clipboard Class and file it with what is in the OS Clipboard (image, RawData, Sound, Text, Video… whatever).

Did you add cb.Close after changing the .Text contents ?
If you do not do that, your change is not validated.

Using a global variable to deal with it in all parts of your program ?
A bad idea in the case you wrote.

The variables are using space for nothing while your program runs and it is not used.

Also, it is a way to get troubles if you only have to use a data (or a Clipboard) in a few location. You may change the variable contents in a method and think its value is the same elsewhere.

The usage is to create a date (a Clipboard) Class when you need it, close it before leaving the method.

This can be false for the data base classes…

Okay, the clipboard.close might be it!

In the documentation it says that is only for Windows…
“Closes the Clipboard which enables other Clipboard objects or other programs to access the Clipboard’s data. This function only affects Windows and is automatically called by the Clipboard’s Destructor.”

Luckily, the this validation is not needed for any string manipulation…

Yes, but your posted code works. Try it in an empty project. I bet you have a mistake in your real code, as setting .Text clearly overrides the previous value. Without calling Close().

That is when you dim the clipboard in a method as locale, not in App or in a Module. In that case the Destructor is never called until you quit the application.

I also had hard times today…

Ok, tried cb.close after after assigning cb.text =, but it didn’t work…

And yes, I 'dim’ed the new clipboard in a method in App. That’s causing it??

So, I dim it before each usage in the methods (in App) and use a close thereafter.
It works now perfectly, but I don’t understand it fully (contrary to you guys, obviously…)

Nice (it now works.)

the bug is that if you open a messagedialog after new cliboard instance the text property clears

try this code:

Dim c as New Clipboard
If c.TextAvailable then


Dim d as New MessageDialog                  //declare the MessageDialog object
Dim b as MessageDialogButton                //for handling the result
d.icon=MessageDialog.GraphicQuestion     //display warning icon
d.CancelButton.Visible=True                 //show the Cancel button

d.Message="Would you append clipboard?"
d.Explanation="Should be irreversibile. "

b=d.ShowModal                              //display the dialog
Select Case b                              //determine which button was pressed.
Case d.ActionButton

TEXTAREA.text = c.Text
end select

end if

Thanks. Interesting. So, this is wanted or a Xojo bug?
Anyway, I use now ‘cb = new clipbaord’ anytime before I want something from it, and this seems to work.

[quote=354125:@Peter Kronenberg]Thanks. Interesting. So, this is wanted or a Xojo bug?
Anyway, I use now ‘cb = new clipbaord’ anytime before I want something from it, and this seems to work.[/quote]

Unfortunately i think it’s a bug because before I’ll destroy an object i expect that it doesn’t change outside of my will

I’s outside every object oriented program language logic