【问题标题】:Getting and Setting CKEditor HTML in a C#.net Winform App在 C#.net Winform 应用程序中获取和设置 CKEditor HTML
【发布时间】:2015-09-16 03:35:26
【问题描述】:

我正在尝试在 Winform 应用程序中使用 CKEditor 在 Sql Server 数据库中加载、操作和存储 HTML 内容。

我发现了这篇关于如何将 CKEditor 放到 winform 上的精彩帖子:

Can the CKEditor be used in a WinForms application for (X)HTML editing?

但它没有详细说明如何加载 HTML 内容或在用户操作后提取它。

我试过了:

webBrowser1.Document.GetElementById("editor1").InnerText;

但是返回的是加载到编辑器中的初始数据,在被用户操作之后,它仍然返回相同的值。

如果有人可以扩展上面链接中的答案,并使用一些代码加载编辑器和一些内容,以及一些代码在 WinForm 应用程序中显示一个消息框及其当前内容,我将不胜感激。我怀疑它在最初打开的 html 文件中需要更多的 JavaScript,但我对 javascript 一无所知,我花了几天时间试图通过它但没有成功。

提前致谢。

【问题讨论】:

    标签: javascript c# html winforms ckeditor


    【解决方案1】:

    在今天的更多研究中,我能够回答我自己的问题...... 我加载到 webbrowser 控件的空白 HTML 设置文件如下所示:

    <html>
        <head>
            <script src="http://cdn.ckeditor.com/4.4.7/full/ckeditor.js"></script>
        </head>
        <script>
            var editor1, html = '';
    
            function createEditor() {
                if ( editor1 )
                    return;
    
                // Create a new editor instance inside the <div id="editor1"> element,
                // setting its value to html.
                var config = {};
                editor1 = CKEDITOR.appendTo( 'editor1', config, html );
            }
    
            function getHtml() {
                if ( !editor1 )
                    return;
                html = editor1.getData();
                return html
    
            }
    
            function setHtml(vsHtml) {
                if ( !editor1 )
                    return;
                editor1.setData(vsHtml)
            }
        </script>
        <div id="editor1"></div>
    </html>
    

    C# 代码如下所示:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace HTMLEditor
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load_1(object sender, EventArgs e)
            {
                webBrowser1.Navigate("C:\\AMResearch\\HTMLEditor\\blank.html");
                Application.DoEvents();
            }
    
            private void button1_Click_1(object sender, EventArgs e)
            {
                webBrowser1.Document.InvokeScript("createEditor");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                string sHtml;
                sHtml = (string)webBrowser1.Document.InvokeScript("getHtml");
                MessageBox.Show(sHtml);
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                Object[] objArray = new Object[1];
                objArray[0] = "<p>Hellow World!</p>";
                webBrowser1.Document.InvokeScript("setHtml", objArray);
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      相关资源
      最近更新 更多