Problem pasting HTML from selection in web browser
I am putting together a WYSIWYG html editor using the .NET webbrowser control. I have managed to piece together the code for inserting a table at the cursor position. However I am now trying to do something a little different.
What I need to do is insert a and html comment around a table row. I've managed to write the code that, from the cursor position, looks to the parent elements to find the table row the curor is sat in. The problem is when I try to use the pastHtml method in the range, it throws an exception with no detail on why its failed!.
Code listed below.
private void OnTest(object sender, EventArgs e)
{
try
{
IHTMLDocument2 doc = wbDesign.Document.DomDocument as IHTMLDocument2;
IHTMLSelectionObject currentSelection = doc.selection;
if (currentSelection != null)
{
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
bool foundRow = false;
IHTMLElement parentElement = null;
parentElement = range.parentElement();
if ("TR" == parentElement.tagName) foundRow = true;
while (!foundRow)
{
if (null == parentElement.parentElement) break;
parentElement = parentEleme开发者_StackOverflownt.parentElement;
if ("TR" == parentElement.tagName) foundRow = true;
}
if (foundRow)
{
currentSelection.clear();
range.moveToElementText(parentElement);
range.pasteHTML("test");
}
else
{
MessageBox.Show("Unable to find table row from selection point.");
}
}
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
精彩评论