使用 webbrowser 和 richtextbox 的 C# WYSIWYG 编辑器

发布于 2024-07-25 19:21:39 字数 1783 浏览 6 评论 0原文

我试图按照这里和其他地方的一些示例,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱你一生 2024-08-01 19:21:40

我最近花了太多时间研究 WebBrowser 控件和相关事物的工作原理,这对您来说是一件好事:-)

做您想做的事情,而不是

webBrowserWYSIWYG.DocumentText = rtbWYSIWYG.Text;

webBrowserWYSIWYG.Document.Write(rtbWYSIWYG.Text);

希望这有帮助。 这个对我有用。

编辑:试试这个:

webBrowserWYSIWYG.Document.OpenNew(true);
webBrowserWYSIWYG.Document.Write(rtbWYSIWYG.Text);

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

webBrowserWYSIWYG.DocumentText = rtbWYSIWYG.Text;

do

webBrowserWYSIWYG.Document.Write(rtbWYSIWYG.Text);

I hope that helps. It works for me.

Edit: Try this:

webBrowserWYSIWYG.Document.OpenNew(true);
webBrowserWYSIWYG.Document.Write(rtbWYSIWYG.Text);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文