Love Sun

WebBrowser as an editor control: Make Enter emulate Shift-Enter

My organization’s using a freely available HTML Editor for Windows Forms applications. The product is very solid, does most of what we need to. The one gripe we’ve had since the beginning is the inherent property of the web browser to convert an {ENTER} input to a new paragraph. We wanted to have this keypress emulate a single BR tag (equivalent to a Shift+{ENTER}). This proved to be slightly nontrivial, and we were having trouble finding an online resource that explained how to approach the problem.

After some trial and error and piggy-backing off of some research from a co-worker, it dawned on me that the answer was fairly simple and involved trapping the key event (obviously) and sending a sequence of keys to undo the new paragraph and simply go to the next line (by sending {BACKSPACE}, then Shift+{ENTER}). Within the editor control, add an handler to the PreviewKeyDown event off of the editor web browser, and include this code

 

Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *