gpt4 book ai didi

c# - 使用所见即所得编辑器编辑 HTML

转载 作者:太空狗 更新时间:2023-10-29 16:42:46 24 4
gpt4 key购买 nike

我有一个带有 HTML 字符串的数据 GridView 。使用 CellDoubleClick 事件,我在 WebBrowser 控件中显示 html 字符串。

在 Form1 中

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex != 0 && e.RowIndex != -1)
{
string s = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
this.f2 = new Form2(s);
f2.ShowDialog();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

在 Form2 中

private IHTMLDocument2 doc;
string reply;

public Form2(string reply)
{
InitializeComponent();
this.reply = reply;
}

private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = reply; <--- string from DataGridView

IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;
range.pasteHTML(webBrowser1.DocumentText);
range.collapse(false);
range.select();

doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
doc.designMode = "On";
}

使用上面的代码,我可以成功地将 HTML 字符串显示为纯文本,但是我无法对其进行编辑。或者,如果我使用此代码:

private IHTMLDocument2 doc;
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = reply; <--- string from DataGridView

doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
doc.designMode = "On";

IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;
range.pasteHTML(webBrowser1.DocumentText);
range.collapse(false);
range.select();
}

这将是一张空白表格,但我可以写信给它。

我觉得它与 range.pasteHTML(webBrowser1.DocumentText); 在 Form2_Load 方法中有关,但我不知道有任何其他方法可以让我显示打开 Form2 时来自 DataGridView 的 HTML 字符串。

我想让用户能够将 HTML 字符串编辑为纯文本(之后它将被转换回 HTML 并显示在数据 GridView 中)。

最佳答案

有可能!您可以使用默认的 WebBrowser 控件编辑 HTML,

  1. 添加对此处提供的“Microsoft.mshtml.dll”文件的引用。

  2. 假设您的 WebBrowser 名为“browser”,将此代码添加到 Form.Load 事件中(browser.Document.DomDocument as mshtml.IHTMLDocument2).designMode = "On";

  3. 调用以下函数来格式化所选文本:


browser.document.ExecCommand("Bold", false, null);
browser.document.ExecCommand("Underline", false, null);
browser.document.ExecCommand("Italics", false, null);
browser.document.ExecCommand("StrikeThrough", false, null);
browser.document.ExecCommand("FontName", false, "Times New Roman");
browser.document.ExecCommand("FontName", false, "Arial");
browser.document.ExecCommand("FontName", false, "etc.");
browser.document.ExecCommand("FontSize", false, "1");
browser.document.ExecCommand("FontSize", false, "2");
browser.document.ExecCommand("FontSize", false, "3");
browser.document.ExecCommand("InsertUnorderedList", false, null);
browser.document.ExecCommand("InsertOrderedList", false, null);
browser.document.ExecCommand("Cut", false, null);
browser.document.ExecCommand("Copy", false, null);
browser.document.ExecCommand("Paste", false, null);
browser.document.ExecCommand("CreateLink", true, null);

WebBrowser 控件不允许编辑,只能查看网页。它实际上是在幕后运行的 Internet Explorer/Trident 呈现引擎,它解析 HTML 并呈现具有 DOM/JS 支持的最终页面。没有流行的浏览器支持编辑 HTML 页面,IMO,IE 也不支持。

关于c# - 使用所见即所得编辑器编辑 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13410067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com