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

    if ((Keys)e.KeyData == Keys.Enter)
    {
        SendKeys.Send("{BS}"
);
        SendKeys.Send("+{ENTER}"
);
    }

Print | posted @ Monday, December 19, 2005 11:58 PM

Comments on this entry:

Gravatar # re: WebBrowser as an editor control: Make Enter emulate Shift-Enter
by Thomas Lykke Petersen at 1/11/2006 6:08 AM

Excellent. I have been facing the exact same non trivial task of making TX Text Control emulate single BR tag instead of inserting a new paragraph when pressing the {ENTER} key. Your suggested solution worked perfectly.
Gravatar # re: WebBrowser as an editor control: Make Enter emulate Shift-Enter
by Rajesh at 9/15/2006 4:05 AM

Hi,
I have the same issue in my C# application. But the control never come out of the instruction SendKeys.Send("{BS}");
I just replace SendKeys.Send("{BS}") the code with SendKeys.SendWait("{BS}"), so found that the functionality of BackSpace is working continously.

if (e.KeyPressedCode == 13)
{
SendKeys.Send("{BS}");
SendKeys.Send("+{ENTER}");
}


Thanks

Rajesh

Gravatar # WebBrowser as an editor control: Make Enter emulate Shift-Enter
by Rajesh at 9/15/2006 4:10 AM

Hi,

Thanks for you code, It is working perfectly. Previously iwas writing the code inside the KeyDown event. Now it is written inside the PreviewKeyDownEvent.

Thanks a lot

Rajesh
Gravatar # re: WebBrowser as an editor control: Make Enter emulate Shift-Enter
by Emran at 9/21/2006 1:38 PM

Hi,
I am using the same HTML Editor control and I wanted this control featured with "InnerHtml Changed" event so that if InnerHTML is changed, I can save the updated value in my database. IS it possible to do that ? Moreover, Is it possible to make this property "InnerHTML" data boundable so that I can get the benifit of data binding to a data source ?

Regards,
Emran
Gravatar # re: WebBrowser as an editor control: Make Enter emulate Shift-Enter
by Diego at 1/10/2008 10:58 PM

so cool... thanks!!
Gravatar # re: WebBrowser as an editor control: Make Enter emulate Shift-Enter
by Sergey at 8/4/2008 10:34 AM

Awesome! Hundred thanks!
Gravatar # thanks
by jbr at 9/3/2008 3:45 PM

thanks again for the code
Gravatar # re: WebBrowser as an editor control: Make Enter emulate Shift-Enter
by Karin at 10/28/2008 12:03 AM

Very cool thanks, the code works perfectly on the webbrowser control.

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 1 and 6 and type the answer here: