2012-11-16 19 views
5

Ho una vista datagrid con stringhe HTML. Utilizzando un evento CellDoubleClick, sto visualizzando la stringa html in un controllo WebBrowser.Modifica HTML con un editor WYSIWYG

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

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

Utilizzando il codice di cui sopra, posso visualizzare correttamente la stringa HTML come un testo in chiaro, ma non sono in grado di modificarla. In alternativa, se utilizzo questo codice:

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

Sarà un modulo vuoto, ma potrò scrivervi.

Ho la sensazione che abbia a che fare con range.pasteHTML(webBrowser1.DocumentText); nel metodo Form2_Load, ma non conosco nessun altro metodo che mi consenta di visualizzare la stringa HTML da DataGridView all'apertura di Form2.

Desidero consentire agli utenti di modificare la stringa HTML come testo normale (dopo di che verrà convertita in HTML e visualizzata nella vista datagrid).

risposta

1

È possibile! È possibile modificare HTML usando il controllo predefinito WebBrowser,

  1. Aggiungere un riferimento al file "Microsoft.mshtml.dll", disponibile qui.

  2. Assumendo che il browser web si chiama "browser", aggiungere questo codice in caso Form.Load (browser.Document.DomDocument as mshtml.IHTMLDocument2).designMode = "On";

  3. richiamare le seguenti funzioni per formattare il testo selezionato:


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

0 Il controllonon consente la modifica ed è progettato per visualizzare solo le pagine Web. In realtà è il motore di rendering di Internet Explorer/Trident che opera dietro le quinte che analizza l'HTML e rende la pagina finale completa con il supporto di DOM/JS. Nessun browser popolare supporta la modifica di pagine HTML, IMO e nemmeno IE.