使用 webbrowser 和 richtextbox 的 C# WYSIWYG 编辑器
我试图按照这里和其他地方的一些示例,在 C# 中进行所见即所得编辑。
我使用网络浏览器来实现编辑器的设计状态,但我需要能够切换到“html-view”,所以我使用了丰富的文本框,我的想法是从网络浏览器中获取内容并将其设置为RTB,反之亦然。
它工作正常,直到我尝试将 RTB 中的值放回到网络浏览器中,然后我得到一个 “此文档已更改,您要保存更改吗”-警报,之后网络浏览器将不再接受新内容。
知道该怎么做吗? 或者还有其他方法来处理我之后的解决方案吗?
代码:
namespace EmailAdmin
{
public partial class Form1 : Form
{
// global variables
private IHTMLDocument2 doc;
private int WYSIWYGviewState = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// initiate web browser to design mode
webBrowserWYSIWYG.DocumentText = "<html><body></body></html>";
doc = webBrowserWYSIWYG.Document.DomDocument as IHTMLDocument2;
doc.designMode = "On";
}
private void linkSwitchWYSIWYGview_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
// determins viewstate
// is design view
if (WYSIWYGviewState == 0)
{
// set html view
WYSIWYGviewState = 1;
rtbWYSIWYG.Visible = true;
// populates the texteditor with html
rtbWYSIWYG.Text = webBrowserWYSIWYG.DocumentText;
// change label text
linkSwitchWYSIWYGview.Text = "View Design";
}
// is html view
else if (WYSIWYGviewState == 1)
{
// set design view
WYSIWYGviewState = 0;
rtbWYSIWYG.Visible = false;
// populates the designer with html
webBrowserWYSIWYG.DocumentText = rtbWYSIWYG.Text;
// change label text
linkSwitchWYSIWYGview.Text = "View HTML";
}
}
}
}
Im trying to hammer togehter a WYSIWYG-edit in c# following some examples from here and other place.
Im using a webbrowser for the design state of the editor, but i need to be able to switch to "html-view" so I used a rich textbox, and my tought was to just grab the content from the webbrowser and set it to the rtb, and the other way around.
It works fine until i try to put back the value from the rtb into the webbrowser, then i get a
"This document has changed, do you want to save the changes"-alert and after that the webbrowser wont accept new content.
Any idea what to do? Or any other way to handle the solution im after?
the code:
namespace EmailAdmin
{
public partial class Form1 : Form
{
// global variables
private IHTMLDocument2 doc;
private int WYSIWYGviewState = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// initiate web browser to design mode
webBrowserWYSIWYG.DocumentText = "<html><body></body></html>";
doc = webBrowserWYSIWYG.Document.DomDocument as IHTMLDocument2;
doc.designMode = "On";
}
private void linkSwitchWYSIWYGview_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
// determins viewstate
// is design view
if (WYSIWYGviewState == 0)
{
// set html view
WYSIWYGviewState = 1;
rtbWYSIWYG.Visible = true;
// populates the texteditor with html
rtbWYSIWYG.Text = webBrowserWYSIWYG.DocumentText;
// change label text
linkSwitchWYSIWYGview.Text = "View Design";
}
// is html view
else if (WYSIWYGviewState == 1)
{
// set design view
WYSIWYGviewState = 0;
rtbWYSIWYG.Visible = false;
// populates the designer with html
webBrowserWYSIWYG.DocumentText = rtbWYSIWYG.Text;
// change label text
linkSwitchWYSIWYGview.Text = "View HTML";
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最近花了太多时间研究 WebBrowser 控件和相关事物的工作原理,这对您来说是一件好事:-)
做您想做的事情,而不是
我
希望这有帮助。 这个对我有用。
编辑:试试这个:
It's a good thing for you that I've spent far too much time recently looking at how the WebBrowser control and related things work :-)
To do what you want, instead of
do
I hope that helps. It works for me.
Edit: Try this: