views:

411

answers:

1

Hi There

I am using the .NET WebBrowser control to as a WYSIWYG html editor. I've been using the ExecCommand to do the formatting function fine so far, however I now want to add a table inserter. The problem is I can only seem to Append the table to the document, not insert it half way through. Below is some basic test code, if anyone can help, I'd appreciate it.

        HtmlElement tableRow = null;
        HtmlElement headerElem = null;

        HtmlDocument doc = wbDesign.Document;
        HtmlElement tableElem = doc.CreateElement("TABLE");
        doc.Body.AppendChild(tableElem);

        HtmlElement tableHeader = doc.CreateElement("THEAD");
        tableElem.AppendChild(tableHeader);
        tableRow = doc.CreateElement("TR");
        tableHeader.AppendChild(tableRow);
        headerElem = doc.CreateElement("TH");
        headerElem.InnerText = "Col1";
        tableRow.AppendChild(headerElem);

        headerElem = doc.CreateElement("TH");
        headerElem.InnerText = "Col2";
        tableRow.AppendChild(headerElem);

        HtmlElement tableBody = doc.CreateElement("TBODY");
        tableElem.AppendChild(tableBody);

        tableRow = doc.CreateElement("TR");
        tableBody.AppendChild(tableRow);
        HtmlElement tableCell = doc.CreateElement("TD");
        tableCell.InnerText = "Test";
        tableRow.AppendChild(tableCell);
        tableCell = doc.CreateElement("TD");
        tableCell.InnerText = "Test";
        tableRow.AppendChild(tableCell);
A: 

You need to navigate the HtmlDocument structure, finding the node at where you want to insert it, and then append there. If you Append to the Body, you'll just add to the end of the last element, i.e. the end.

Amethi
Any idea how you do this based on the current selection?
Ian Ford
you need help finding the last element in the control range of IHTMLDocument2::selection ? it has a length property.
Sheng Jiang 蒋晟
The problem I have is i cannot seem to insert the table into the current selection?!?!
Ian Ford