I'm trying to create a simple text field for WYSIWYG editing. However, I only want to allow certain types of formatting (e.g. Bold, Italic, Underline and a single heading type but no colors or different fonts.)

The issue is if I use an editor that can accept formatting, someone can create or copy formatted text in another program, then simply paste it into the text field and all that formatting goes with it, allowing things I'm not interested in, such as different fonts, colors, etc. I don't want to allow that.

At best, I want to automatically strip out any formatting that I don't support. At worst, I want to simply paste whatever as plain text making them have to reformat it. But in no case do I want to just dump the clipboard to the text area.

Any thoughts on how to do this?

有帮助吗?

解决方案

I would recommend creating a new text field/text area class and creating an EditPaste menu handler that (a) does what you're looking for in terms of handling the clipboard's text and (b) returns true to prevent the default pasting from happening. This is safer than using the Key down events because the user might manually select paste from the edit menu.

You can access the text on the clipboard by creating a Clipboard object.

To subclass the textfield and intercept the paste menu command:

  1. With your Project open, go to Project Menu > Add > Class
  2. Select the new class in the project's tab and in the properties panel set the super to TextField
  3. Double-click on the class to edit it
  4. Click the "Add Menu Handler" mid-toolbar button in your class
  5. Change the menu item name to "EditPaste". Put your code in before the "return true" and be sure to leave the return true in there.

Your code can then format and paste the text manually and override the default paste function.

Any command-V or control-V in that text field will cause that menu handler to fire. Any contextual menus would be added by you anyway since real basic does not create the default contextual menus, so you'd have control over that as well.

To add the text field to a window, just change the filter above the objects list to Project controls, and drag the class in from there.

其他提示

You could intercept the paste yourself by intercepting it in the KeyDown events. Then, you could look to parse it yourself. That could be kind of tricky but I think that's about the only way you could do it.

It might just be easier to parse the resulting StyleRun after the paste and strip out formatting you don't want.

Alternately, you could look at the Formatted Text Control from True North Software and override the paste methods of the control (you get all the source) and just handle it yourself.

Either way, I think it will be a fair amount of work.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top