clipboard function results in error when I close my app

I have a listbox with various columns, the last 2 are for a username and a password. I have set those cells to have a contextmenu and contextmenuaction. The only function in both of there cells is to copy the text into the clipboard. For some reason, after I’ve actually used one of those copy functions in my running app, when I close the app, I get this error message:

Runtime error Press OK to continue Press Cancel to quit Please reoprt what caused this error along with the information below Universal\\REALstring.cpp: 130 Failure condition: usageCount

Here’s what’s in ConstructContextMenu

base.Append( new MenuItem( "Copy" ) )

Here’s what’s in ContextualMenuAction

[code]If hitItem <> Nil Then
Dim c As New Clipboard
c.Text = hititem.text
c.Close
end if

Return true[/code]

BTW: this happens with both 32 bit and 64 bit builds. Documentation says I’m doing this right.
Any help would be greatly appreciated.

I’m curious, does it still crash if you don’t close it when you’re done?

you want cell text in clipboard ?

[code]If hitItem.text = “copy” Then
Dim c As New Clipboard
c.Text = ListBox1.Cell(x, x)
c.Close
end if

Return true[/code]

But the clipboard is emptied when you close a Xojo app.
That is unfortunately the case.

Thanks for your replies.
Greg: No. It doesn’t crash until I close it.
Axel: The copy function works fine as I’ve shown above. Are you saying that your example will not cause the error? I actually can’t adopt your method, since the cell text is different in every cell. Thanks though. :slight_smile:

[quote=400566:@Axel Schneider]But the clipboard is emptied when you close a Xojo app.
That is unfortunately the case.[/quote]

I’m not experiencing this in my Apps.

Me either.

BTW: I see this error from time to time in one of my Apps, but it may be related to Encoding issues in my Case.

I don’t know if this will help, but maybe you need to Define/Convert the Encoding of the Cell-Text. If if Text generated within your code and then written into the Cells, it will be UTF8 and should work without any define/converts.

[quote=400620:@Sascha S]BTW: I see this error from time to time in one of my Apps, but it may be related to Encoding issues in my Case.

I don’t know if this will help, but maybe you need to Define/Convert the Encoding of the Cell-Text. If if Text generated within your code and then written into the Cells, it will be UTF8 and should work without any define/converts.[/quote]
Cell text is populated from an sqlite db file. I have not had this issue with any other apps, which is why I’m so puzzled. I’ll look into the encoding, thanks.

ok, I used a function from the docs to retrieve the encoding of hitItem.Text.

Dim t As TextEncoding t = Encoding(hitItem.Text) If t <> Nil then msgbox("Base=" + Str(t.Base) + " Format=" + Str(t.Format) + " Variant=" + Str(t.Variant)) End If
I got the following results, which I completely don’t understand:

Base = 256 Format = 2 Variant = 0

I also tried setting the encoding to UTF8, but still got the same error when closing the app.

hitItem.Text = DefineEncoding(hitItem.Text, Encodings.UTF8)

BTW: when I commented out my copy to clipboard function and just used the MsgBox display of encoding, I got no error closing the app. This can only mean that something in my copy function is wrong…right? Thing is, I have used this same function in so many other apps with no problem. I am so confused.

[quote=400645:@Bill Dawson]I got the following results, which I completely don’t understand:

Base = 256 Format = 2 Variant = 0[/quote]

Here’s an explanation of the values

Base is f.e. a UnicodeDefault :wink:

BTW: This will only work if Xojo knows the Encoding of the Text.

I’d recommend to define the Encoding of the Cell Text when you read it from the Database.
Later then (maybe) convert it to UTF8 before copying it into the Clipboard (i am not sure if this step is really needed…)

Oh! And another one already mentioned by @Axel Schneider : hitItem.Text is NOT the Cell Text Content :wink:

It’s the case in my Linux. (Mint 18.3)

[quote=400649:@Sascha S]BTW: This will only work if Xojo knows the Encoding of the Text.

I’d recommend to define the Encoding of the Cell Text when you read it from the Database.
Later then (maybe) convert it to UTF8 before copying it into the Clipboard (i am not sure if this step is really needed…)[/quote]
That’s exactly what I tried. No effect.

I’m not sure what you mean…I use hitItem and the text is copied and pasted perfectly.

I actually thought you want the cell text?

that would be ListBox1.Cell(row, column) not hititem.text

[quote=400663:@Axel Schneider]I actually thought you want the cell text?

that would be ListBox1.Cell(row, column) not hititem.text[/quote]
Yes, I do want the cell text. What I have works, but produces and error on app close. I’ll try it your way and see if that cures the problem. Thank you.

That’s not really what I asked. Try not closing the clipboard at all.

On Windows, this often leads to a blocked Clipboard. That means, no App can Copy&Paste anymore afterwards.

That works! Thanks man! I’m still going to work on using Axel’s suggestion and see how that goes, but this is definitely 1 solution.
Sascha: I’ll be watching for the blocked clipboard you mentioned. Thanks for the tip.