WebBrowser给表格增加删除行

1.修改窗体: \UI\DocInfo\Document.cs


        private void menuTableAdd_Click(object sender, EventArgs e) {
            HtmlEditor.TableAddRow(htmlDesign);
        }

        private void menuTableDel_Click(object sender, EventArgs e) {
            HtmlEditor.TableDeleteRow(htmlDesign);
        }

2.修改类:HtmlEditor.cs


        public static void TableAddRow(WebBrowser pWb) {
            int rowIndex = 0;
            HTMLTable table = GetSelectTable(pWb, ref rowIndex);
            if (table != null) {
                int colLen = table.cells.length / table.rows.length;
                HTMLTableRow row = (HTMLTableRow)table.insertRow(rowIndex);
                for (int i = 0; i < colLen; i++) {
                    object elCell = row.insertCell(i);
                    HTMLTableCell cell = (HTMLTableCell)elCell;
                    cell.innerText = "";
                }
            }

        }
        public static void TableDeleteRow(WebBrowser pWb) {
            int rowIndex = 0;
            HTMLTable table = GetSelectTable(pWb, ref rowIndex);
            if (table != null) {
                table.deleteRow(rowIndex);
            }
        }

        public static HTMLTable GetSelectTable(WebBrowser pWb,ref int pRowIndex) {
            mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)pWb.Document.DomDocument;
            mshtml.IHTMLTxtRange rangeObj = (mshtml.IHTMLTxtRange)(doc2.selection.createRange());

            mshtml.IHTMLElement ele = rangeObj.parentElement();
            int rowIndex = 0;
            if (ele != null) {
                for (int i = 0; i < 100; i++) {
                    if (ele == null) break;
                    if (ele.tagName.ToLower() == "table") {
                        break;
                    }
                    if (ele.tagName.ToLower() == "tr") {
                        HTMLTableRow tr = (HTMLTableRow)ele;
                        rowIndex = tr.rowIndex;
                    }
                    ele = ele.parentElement;
                }
            }
            if (ele != null && ele.tagName.ToLower() == "table") {
                pRowIndex = rowIndex;
                return (HTMLTable)ele;
            }
            else {
                return null;
            }
        }


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。