Preventing Cut and Paste into a Textarea?

I’ve been searching but only coming up with posts that are several years old and so far, lots of suggestions that most people are saying don’t work or have issues.

I want to allow typing in the text area, but not cut and paste via any of the typical methods. In other words, no CTRL-V, No menu paste, no drag and drop, etc.

Is this possible? Anyone have suggestions?

You want to rename the edit menu to prevent all cut and paste, or just the submenu paste.

That will effectively also disable the keyword shortcuts.

Drag and drop is a different beast altogether. You may be able to deal with it in the dropobject event handler.

I just went into the Edit menu and made the paste option not visible and removed the Shortcut key, that did prevent pasting with the menu or control v. The problem is, that it’s for the entire app. I really only want to disable for a specific text box.

I will look at the drop object to prevent it there if possible.

it is also possible to add a “edit-paste” and “edit-copy” menu handler to a subclass of textarea
and return true to tell you deal with the copy and paste.
then try to override the “dropobject” event and return true also.

1 Like

I’ve tried the subclass of the menu handler and didn’t seem to prevent it. Not having any luck with the drop object either. It actually blows my mind that there isn’t a simple way built in to do this.

I’m hoping someone has some actually code examples of how do accomplish this.

I did subclass the editpaste menu handler yesterday for a textaera subclass and I have been able to wipe the styled text infos to paste only the text without style. trust me it works.
it was on xojo 2019r11 though. pretty sure it still works in 2021r3.

Function EditPaste() As Boolean
  If mPasteOnlyText Then
    Dim c As New Clipboard
    Dim ct As String = c.Text
    c.Close
    c = New Clipboard
    c.Text = ct
    c.Close
  End If
  
  Return False
  
End Function

It is possible to attach a different menubar to a window.

Yes, but I guess Steve is looking for having just one special field among probably others.
Jean-Yves’ answer is the best approach, IMO.

That is true Arnaud. I only want the limitations on the one control, the textarea box. So far, I can’t seem to get anything to prevent cut and paste to it. If it doesn’t prevent ALL methods of pasting to the textarea it won’t meet my requirements.

I’ve not tried this, but what about setting it readonly, then using KeyDown to accept the character and add it to the TextArea?

On Windows, unless it has changed since I saw it, a read-only field loses its insert point; it’d become hard for the user to know where he’s going to write.

try this project example

1 Like

XoJo is not recognizing the file. It’s either corrupted, missing something or I’m missing something.

I just download the file and rename the xojo-binary-project to xojo_binary_project. basically replace the - with _

I’ll give that a try. Did you get it to work?

It works as you requested:

  • no paste (keyboard or menu)
  • no drag text
    even no cut/no copy from the TextArea.

Tested on mac.

yes I did… I got it working after renaming the file extension

Thanks. I just tested on my Mac and windows and it works. Now I just need to review the code a bit and understand how to add the needed sections to my project.

Thanks! This does seem to be working for what I need. I’ll look into adding the appropriate code to my project.

sorry for the buzz with this file extension !
next time I will zip the file it should be better.

1 Like