2012-11-16 171 views
5

我有一個HTML字符串一個DataGridView。使用CellDoubleClick事件,我在WebBrowser控件中顯示html字符串。編輯HTML與WYSIWYG編輯器

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

在窗體2

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並顯示在DataGridView)。

回答

1

它的可能!你可以使用默認的WebBrowser控制編輯HTML,

  1. 添加一個參考「Microsoft.mshtml.dll」文件,可在這裏。

  2. 假設你的web瀏覽器被命名爲 「瀏覽器」,在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); 

The WebBrowser控件不允許編輯,並且被設計成只查看網頁。它實際上Internet瀏覽器/三叉戟渲染引擎,解析HTML並呈現最後一頁完整的DOM/JS支持在幕後操作。沒有流行的瀏覽器支持編輯HTML頁面,IMO,IE也不支持。