Webbrowse如何禁用Backspace(返回)
1. Webbrowse属性WebBrowserShortcutsEnable:指定在WebBrowser控件中是否启用某些快捷键,但Backspace不在控制范围,该属性无法实现禁用Backspace(返回)
2. Webbrowse事件PreviewKeyDown:不支持e.handled=true的属性,依然无法实现禁用Backspace
3. 使用HTMLDocument 的onkeydown 事件:
代码
private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
mshtml.HTMLDocument doc1 = (mshtml.HTMLDocument)axWebBrowser1.Document .DomDocument;
mshtml.HTMLDocumentEvents2_Event eve = (mshtml.HTMLDocumentEvents2_Event)doc1;
eve.onkeydown += new mshtml.HTMLDocumentEvents2_onkeydownEventHandler(eve_onkeydown);
}
void eve_onkeydown(mshtml.IHTMLEventObj pEvtObj)
{
if (pEvtObj.keyCode == 8) //backspace
{
pEvtObj.cancelBubble = true;
}
}
{
mshtml.HTMLDocument doc1 = (mshtml.HTMLDocument)axWebBrowser1.Document .DomDocument;
mshtml.HTMLDocumentEvents2_Event eve = (mshtml.HTMLDocumentEvents2_Event)doc1;
eve.onkeydown += new mshtml.HTMLDocumentEvents2_onkeydownEventHandler(eve_onkeydown);
}
void eve_onkeydown(mshtml.IHTMLEventObj pEvtObj)
{
if (pEvtObj.keyCode == 8) //backspace
{
pEvtObj.cancelBubble = true;
}
}
ps:需要using mshtml; 右键解决方案资源管理器-》添加引用-》.NET-》Microsoft.mshtml
ref:
http://vbcity.com/forums/t/147562.aspx
http://www.cnblogs.com/taobataoma/archive/2007/04/30/733242.html