0

I have a .net Windows forms project that includes the System.Windows.forms.WebBrowser control to allow the user to do some editing of HTML content. When this control is in edit mode Elements such as div or span can be drag-and-drop edited, but selecting an element and typing Delete does nothing.

I have seen a few posts that talk about making this work in C++ but they are not very detailed. Example http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/1f485dc6-e8b2-4da7-983f-ca431f96021f/

This next post talks about using a function called TranslateAccelerator method to solve similar problems in MFC projects. http://vbyte.com/iReader/Reader.asp?ISBN=0735607818&URI=/HTML/chaab.htm

Does anyone have a suggestion on how to make the delete key work in C# or VB for a windows forms project?

Here is my code to create the WebBrowser content:

WebBrowser1.Navigate("about:blank")   ' Initializes the control
Application.DoEvents
WebBrowser1.Document.OpenNew(False).Write("<html><body><span>Project Title</span><input type='text' value='' /></body></html>")
WebBrowser1.ActiveXInstance.Document.DesignMode = "On"  ' Option Explicit must be set to off
WebBrowser1.Document.Body.SetAttribute("contenteditable", "true")

Thanks

nuander
  • 1,319
  • 1
  • 19
  • 33
  • Strange code, avoid vb.net to C# converters when posting snippets. You'll need a ` – Hans Passant Jul 27 '12 at 02:06
  • OK, the code above works in VB. The last line is not necessary. For a C# example see below. – nuander Jul 27 '12 at 21:34

2 Answers2

2

Well the problem was that one of the control properties, "WebBrowserShortcutsEnabled" was set to false. Thanks everyone for your help, there is no way anyone could have guessed that so I get a big "DUH!". I did find a way to make this work in c# where the code would look like this:

public Form1() {
    InitializeComponent();
    webBrowser1.Navigate("about:blank");  // Initializes the webbrowser control
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
    mshtml.IHTMLDocument2 doc = webBrowser1.Document.DomDocument as mshtml.IHTMLDocument2;
    doc.designMode = "On";
    webBrowser1.Document.OpenNew(false).Write(@"<html><body><span>Project Title</span><input type=""text"" value="""" /></body></html>");
}

...assuming that a reference had been added to MSHTML. The documentCompleted event accomplishes the same thing as the Application.DoEvents in my first code exmaple, so that could go either way.

nuander
  • 1,319
  • 1
  • 19
  • 33
0

I just tried this method:

webBrowser1.Navigate(@"javascript:document.body.contentEditable='true'; document.designMode='on'; void 0");

Elements can be dragged and deleted, you can also edit text with a double click.

Alex Butenko
  • 3,664
  • 3
  • 35
  • 54
  • Yes but can you delete the selected HTML element using the delete key? – nuander Jul 26 '12 at 18:53
  • That did not fix my problem. I will update my post with a specific example. – nuander Jul 26 '12 at 23:13
  • "WebBrowser1.ActiveXInstance.Document.DesignMode = "On";" does not compile. Why can not you do it via javascript? – Alex Butenko Jul 27 '12 at 11:57
  • This project I was given is in VB and when option explicit is off this compiles as described here http://social.msdn.microsoft.com/Forums/eu/winforms/thread/f4025437-2b01-4e7c-b9b1-283b39c9779b In any case i created a new c# project and used your code and it definately is NOT in design mode. How did you add html elements to your document? Thx – nuander Jul 27 '12 at 20:14