RichEditControl控件的ContentChanged事件执行两次的解决办法

将近没事,在学习Devexpress.用到了RichEditControl控件,当在加载一个件时,发现ContentChanged事件被触发了两次。

想了半个小时也没想原来来。于是去官网下载官方文档查看,恍然大悟。原谅如下:

Note that methods such as DevExpress.XtraRichEdit.API.Native.SubDocument.InsertText and DevExpress.XtraRichEdit.API.Native.SubDocument.InsertDocumentContent result in significant changes of the internal document model. The ContentChanged event occurs two times in this case, and you cannot predict when the second ContentChanged event fires.

To distinguish between loading a new document and a situation when the document is modified, use the Modified property within the event handler. When a document is newly created using the CreateNewDocument method, loaded from a file or from a stream, or created by assigning a value to a certain text property, the Modified value is false. Otherwise, it is set to true.

 

意思就是说:在用到InsertText和InsertDocumentContent等方法时会导致文档内部结构发生变化。在这种情况下,ContentChanged事件会被触发两次,并且不能预测

第二次在什么时候引发。

为了区分是加载一个新文档还是文档被改变,在事件处理中可以使用Modified属性。当一个文档通过CreateNewDocument 方法 、从文件或者流中加载、或者

通过指定某些值给一些text属性创建时,Modified属性值为false,其他情况下均为true.

 

也就是说在事件处理中,可以加入以下代码:

    if (this.richEditControl1.Modified)  //非新创建文件,内容发生更改时
            {
                //doSomething
            }

    else  //新创建的文件

    {

      //doSomething

    }

 

posted @ 2014-11-04 17:33  小指  阅读(1080)  评论(0编辑  收藏  举报