DXperience之RichEditControl控件Bug解决方法

来源:互联网 发布:北京软件就业培训 编辑:程序博客网 时间:2024/05/17 20:30

由Devexpress公司开发的控件套装DXperience中有不少组件有Bug,如TabPage得多线程问题,这里要说的时RichEditControl控件的滚动条的问题。

Bug现象:
代码:
     RichEditControl ric=new RichEditControl();
     ric.Enabled=false;
     ……..
     ric.Enabled=true;

设置RichEditControl控件Enabled属性为false,然后在重新设置为true之后RichEditControl控件的滚动条将处于禁用状态,用调试器查看滚动条的Enabled属性为false,所以确定在重新设置为true的时候滚动条并未被置为true,用Reflector查看相关代码为:
protected override void OnEnabledChanged(EventArgs e)
{
    base.OnEnabledChanged(e);
    if (base.Enabled && this.Focused)
    {
        this.DestroyCaretTimer();
        this.InitializeCaretTimer();
        this.ShowCaretCore();
        this.VerticalScrollBar.Enabled = true;
        this.HorizontalScrollBar.Enabled = true;
    }
    else
    {
        this.DestroyCaretTimer();
        this.HideCaretCore();
        this.VerticalScrollBar.Enabled = false;
        this.HorizontalScrollBar.Enabled = false;
    }
    this.OnUpdateUI();
}
问题出在base.Enabled && this.Focused上,this.Focused一直为false,因此if块代码并未被执行,所以滚动条的Enabled始终为false
Bug解决办法:
1.  使得Focused值为true
2.  使得VerticalScrollBar和HorizontalScrollBar控件的Enabled为true
这里采用第二种方式解决,由于RichEditControl控件并未暴露任何有关滚动条的属性或方法,且访问性为非protected,因此直接访问设置或者通过继承的方式是无法做到的,所以必须采用反射的方式才能达到这个目的,这里还必须使用二次反射,第一次反射查找到VerticalScrollBar和HorizontalScrollBar属性,然后在通过反射取得Enabled属性,这里也可以直接通过强制转换通过GetValue函数获取的对象为指定的类型,然后直接设置Enabled为true,代码如下所示:
        private void SetEnable()
        {
            Type type = ric.GetType(); //ric即RichEditControl实例
            PropertyInfo info = type.GetProperty("VerticalScrollBar", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
            ScrollBarBase vs = (ScrollBarBase)info.GetValue(ric, null);
            vs.Enabled = true;
            info = type.GetProperty("HorizontalScrollBar", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
            vs = (ScrollBarBase)info.GetValue(ric, null);
            vs.Enabled = true;
        }
   通过这段代码就可以解决RichEditControl控件的滚动条不可用的bug
以上方法也可用于其它操作,比如要操作一个类的非public函数或者属性,甚至操作类中的非public组合类的函数或属性等,有些时候由于设计需要将某些函数属性设计为外部不可访问的,但有些情况又需要访问,即可通过反射来访问.

原创粉丝点击