iceman 发表于 2012-5-9 16:30:00

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

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.Length || currentColumn == this.textControl1.Lines.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.Length - 1);
                  }
                }
                else
                {
                  this.textControl1.InputPosition = new TXTextControl.InputPosition(currentPage, currentLine, currentColumn - 1);
                }
            }
      }Demo 下载:
测试环境:TX Control .NET 17.0 && VS 2010

ZenosZeng 发表于 2012-5-9 17:38:00

帅,太需要该功能的实现方法了

iceman 发表于 2012-5-9 17:47:00

回复 2# dof 的帖子

:D
页: [1]
查看完整版本: TX Text Control 模仿 MSWord 修改记录功能