TX Text Control 是一款功能丰富的文字处理控件,它以可重复使用控件的形式为开发人员提供了Word中常用的文字处理功能,对于需要强大且灵活的文档处理能力的应用程序而言,是理想的选择。

这篇文章就介绍怎样实现简单的 MSWord 修改记录功能。

1.通过 textControl1_KeyDown 事件捕捉 Delete 键:

 
private void textControl1_KeyDown(object sender, KeyEventArgs e)
        {
            //Delete
            if (e.KeyValue == 46 )
            {
                e.Handled = true;

                int currentLine = this.textControl1.InputPosition.Line;
                int currentColumn = this.textControl1.InputPosition.Column;
                int currentPage = this.textControl1.InputPosition.Page;

                int start = this.textControl1.InputPosition.TextPosition;
                int length = 1;

                this.textControl1.Selection.Start = start;
                this.textControl1.Selection.Length = length;
                this.textControl1.Selection.Strikeout = true;
                this.textControl1.Selection.ForeColor = Color.Red;


                TXTextControl.TextField delTextField = new TXTextControl.TextField();
                delTextField.ID = delID;
                delID++;
                delTextField.Text = this.textControl1.Selection.Text;

                this.textControl1.Selection.Text = "";

                this.textControl1.TextFields.Add(delTextField);
                DelTextFieldCol.Add(delTextField);

                if (currentColumn == this.textControl1.Lines[currentLine].Length || currentColumn == this.textControl1.Lines[currentLine].Length - 1)
                {
                    if (currentLine == this.textControl1.Lines.Count)
                    {
                        this.textControl1.InputPosition = new TXTextControl.InputPosition(currentPage, currentLine, currentColumn);
                    }
                    else
                    {
                        this.textControl1.InputPosition = new TXTextControl.InputPosition(currentPage, currentLine + 1, 0);
                    }
                }
                else
                {
                    this.textControl1.InputPosition = new TXTextControl.InputPosition(currentPage, currentLine, currentColumn + 1);
                }
            }
        }
复制代码

2.通过 textControl1_KeyPress 事件捕捉 Backspace 键:

 
private void textControl1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Backspace
            if (e.KeyChar.ToString() == "\b")
            {
                e.Handled = true;

                int currentLine = this.textControl1.InputPosition.Line;
                int currentColumn = this.textControl1.InputPosition.Column;
                int currentPage = this.textControl1.InputPosition.Page;

                int start = this.textControl1.InputPosition.TextPosition;
                int length = 1;

                this.textControl1.Selection.Start = start - 1;
                this.textControl1.Selection.Length = length;
                this.textControl1.Selection.Strikeout = true;
                this.textControl1.Selection.ForeColor = Color.Red;

                TXTextControl.TextField delTextField = new TXTextControl.TextField();
                delTextField.ID = delID;
                delID++;
                delTextField.Text = this.textControl1.Selection.Text;

                //this.textControl1.Selection.Text = "";

                this.textControl1.TextFields.Add(delTextField);
                DelTextFieldCol.Add(delTextField);

                if (currentColumn == 0)
                {
                    if (currentLine == 1)
                    {
                        this.textControl1.InputPosition = new TXTextControl.InputPosition(currentPage, currentLine, currentColumn);
                    }
                    else
                    {
                        this.textControl1.InputPosition = new TXTextControl.InputPosition(currentPage, currentLine - 1, this.textControl1.Lines[currentLine - 1].Length - 1);
                    }
                }
                else
                {
                    this.textControl1.InputPosition = new TXTextControl.InputPosition(currentPage, currentLine, currentColumn - 1);
                }
            }
        }
复制代码

环境:TX Control .NET 17.0 && VS 2010

TXTrackChanges.zip (44.34 K, 下载次数:59)