小弟使用WebBrowser开发HTML编辑器是当为WebBrowser的DocumentText属性赋值后,就无法添加/编辑WebBrowser中的内容了!!
在赋值的时候添加xxx.Document.ExecCommand("ModeSize", false, null);也无法进入编辑模式。
在赋值的时候添加xxx.Document.ExecCommand("ModeSize", false, null);也无法进入编辑模式。
调试欢乐多
webBrowser1.DocumentText = string.Empty; webBrowser1.Document.ExecCommand("EditMode", false, null);//设置为编辑状态 webBrowser1.Document.ExecCommand("LiveResize", false, null);
/// <summary>
/// 初始化WebBorwer控件的编辑功能
/// </summary>
private void OnInitHtmlEditor()
{
if (this.DesignMode == false)
{
this.m_WebBrower.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(m_WebBrower_DocumentCompleted);
}
this.m_WebBrower.DocumentText = "<html><Body></Body></html>";
this.doc = this.m_WebBrower.Document.DomDocument as IHTMLDocument2;
if (!this.m_ReadOnly)
{
// 当该控件进入编辑模式后,文档一旦改变就会弹出保存对话框
// 所以在对DocumentText属性赋值后,调用this.m_axHtmlEditor.AxBorwse.Document.OpenNew(true);即可。
this.doc.designMode = "On";
}
this.m_tsForeColor.EditValue = Color.Black;
// 等待完成初始化,否则Body属性为空
while(this.m_WebBrower.Document.Body == null)
{
Application.DoEvents();
} if (!this.DesignMode)
{
this.AxHTMLDocument.ExecCommand("FontName", false, this.m_tsFontName.EditValue);
this.AxHTMLDocument.ExecCommand("FontSize", false, this.m_tsFontSize.EditValue);
}
}
解决方法,我就这样搞定的
webBrowser1.Navigate("about:blank");
webBrowser1.Document.Write(string.Empty);
webBrowser1.Document.ExecCommand("EditMode", false, null);//设置为编辑状态
webBrowser1.Document.ExecCommand("LiveResize", false, null);
而不用webBrowser1.DocumentText=value;
我郁闷了好几天了,终于解决了,真心感谢~~~~哈哈,不用客气,我之前也试过好长时间~ 不过用Document.Wirte()之前,还是先要给Documnet
赋一个值,要不Document为null会报错了~
现在终于解决了,爽歪歪:webBrowser1.Document.Write();
看来CSDN的牛人还是有时间理我们的