TX Text Control 模仿 MSWord 修改记录功能

发布时间:2012/11/16 00:11 发布者:葡萄城产品团队

返回博客中心

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)

关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网