简体   繁体   English

如何使Delete键在WebBrowser控件中工作

[英]How do I make the Delete key work in the WebBrowser control

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. 我有一个.net Windows窗体项目,其中包含System.Windows.forms.WebBrowser控件,允许用户对HTML内容进行一些编辑。 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. 当此控件处于编辑模式时可以拖放编辑div或span等元素,但选择元素并键入Delete不会执行任何操作。

I have seen a few posts that talk about making this work in C++ but they are not very detailed. 我看过几篇关于用C ++编写这个工作的帖子,但它们并不是很详细。 Example http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/1f485dc6-e8b2-4da7-983f-ca431f96021f/ 示例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. 下一篇文章讨论了如何使用一个名为TranslateAccelerator方法的函数来解决MFC项目中的类似问题。 http://vbyte.com/iReader/Reader.asp?ISBN=0735607818&URI=/HTML/chaab.htm 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? 有没有人建议如何使删除键在C#或VB中用于Windows窗体项目?

Here is my code to create the WebBrowser content: 这是我创建WebBrowser内容的代码:

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 谢谢

Well the problem was that one of the control properties, "WebBrowserShortcutsEnabled" was set to false. 那么问题是其中一个控件属性“WebBrowserShortcutsEnabled”被设置为false。 Thanks everyone for your help, there is no way anyone could have guessed that so I get a big "DUH!". 谢谢大家的帮助,没有人能够猜到这一点,所以我得到了一个很大的“DUH!”。 I did find a way to make this work in c# where the code would look like this: 我确实找到了一种在c#中使用的方法,代码看起来像这样:

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. ...假设已将参考添加到MSHTML中。 The documentCompleted event accomplishes the same thing as the Application.DoEvents in my first code exmaple, so that could go either way. documentCompleted事件在我的第一个代码exmaple中完成与Application.DoEvents相同的事情,因此可以采用任何一种方式。

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. 可以拖动和删除元素,也可以通过双击编辑文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM