c#WYSIWYG编辑器使用webbrowser和richtextbox

时间:2023-01-16 16:02:49

Im trying to hammer togehter a WYSIWYG-edit in c# following some examples from here and other place.

我试图在这里和其他地方的一些例子之后用c#来锤击一下WYSIWYG-edit。

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.

我使用webbrowser作为编辑器的设计状态,但我需要能够切换到“html-view”,所以我使用了一个丰富的文本框,而我的想法就是从webbrowser中获取内容并将其设置为rtb,反之亦然。

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.

它工作正常,直到我尝试将rtb中的值放回到webbrowser中,然后我得到一个“此文档已更改,您是否要保存更改”-alert,之后webbrowser将不接受新内容。

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";
        }
    }
}
}

1 个解决方案

#1


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 :-)

对你来说这是件好事我最近花了很多时间看看WebBrowser控件和相关的东西是如何工作的:-)

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);

#1


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 :-)

对你来说这是件好事我最近花了很多时间看看WebBrowser控件和相关的东西是如何工作的:-)

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);