当前位置: 代码迷 >> VB Dotnet >> RichTextBox重写有关问题
  详细解决方案

RichTextBox重写有关问题

热度:203   发布时间:2016-04-25 02:00:15.0
RichTextBox重写问题
我想要用RTF属性去对输入到RichTextBox里的内容进行格式转换, 比如颜色改变加下划线什么的, 然后重写了TextChanged方法, 
但是RTF每次赋值就会触发TextChanged事件,这样就死循环了, 然后添加了LockWindowUpdate这个方法,MSDN解释是可以进制窗口刷新的, 但是我添加上去没什么反映?

  Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)

        Dim lockFlag As Boolean = Win32API.LockWindowUpdate(Handle)

        MyBase.OnTextChanged(e)

        Rtf = "{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fcharset134\'cb\'ce\'cc\'e5;}}{\colortbl ;\red255\green0\blue0;\red0\green255\blue0;\red0\green0\blue255;}{\stylesheet{\ulwave;}}" +
                   "\viewkind4\uc1\pard\cf1\f0 \'c2\'e4\cf0\f0\ulwave\'cf\'bc\ulnone\cf3\fs30 \'d3\'eb\cf1\fs32 \'b9\'c2\cf2\f0\fs34 \'f0\'cd\cf3\f0\fs36 \'c6\'eb\cf1\f0 \'b7\'c9\cf2\fs38 \'c7\'ef\cf3\f0 \'cb\'ae\cf3\f0\fs42 \'b9\'b2\cf3\f0\fs44 \'b3\'a4\cf1\f0\fs46 \'cc\'ec\cf2\f0\fs48 \'d2\'bb\'c9\'ab\par}"

        Win32API.LockWindowUpdate(0)

        Invalidate()
    End Sub



------解决思路----------------------
public string Rtf {
            get {
                if (IsHandleCreated)
                {
                    return StreamOut(RichTextBoxConstants.SF_RTF);
                }
                else if (textPlain != null)
                {
                    ForceHandleCreate();
                    return StreamOut(RichTextBoxConstants.SF_RTF);
                }
                else
                {
                    return textRtf;
                }
            }
            set {
                if (value == null) value = "";
 
                if (value.Equals(Rtf))
                    return;
 
                ForceHandleCreate();
                textRtf = value;
                StreamIn(value, RichTextBoxConstants.SF_RTF);
                if (CanRaiseTextChangedEvent) {
                    OnTextChanged(EventArgs.Empty);
                }
            }
        }

http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/RichTextBox.cs,24632c09023b89bf
要不你反射给textRtf 赋值吧,属性里面调用的那么多东西全部都是private或者internal的,就不是给子类用的……