开发者

Using scroll on one memo edit to scroll on another as well

I've got two memo开发者_JS百科edits which are similar (in order to compare two records) I would like to keep the scrolling in synch to ease comparison.

I had originally thought there would be an OnScroll event, but didn't see one, nor anything similar, the closest I saw was Spin, this handles some possibilities, but not all.

I also didn't see a way to navigate the rows.

I did see the ScrollToCaret method, but this doesn't do what I want.

Any ideas?


This can be implemented using reflection only. Here is the code showing how to synchronize vertical scrollbar position:

using System.Reflection;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.ScrollHelpers;

    DevExpress.XtraEditors.ScrollHelpers.ScrollBarEditorsAPIHelper helper1, helper2;

            private void RibbonForm1_Load(object sender, EventArgs e) {
                FieldInfo fi = typeof(MemoEdit).GetField("scrollHelper", BindingFlags.NonPublic | BindingFlags.Instance);
                helper1 = fi.GetValue(memoEdit1) as DevExpress.XtraEditors.ScrollHelpers.ScrollBarEditorsAPIHelper;
                helper2 = fi.GetValue(memoEdit2) as DevExpress.XtraEditors.ScrollHelpers.ScrollBarEditorsAPIHelper;
                helper1.VScroll.ValueChanged += new EventHandler(VScroll_ValueChanged);
                helper2.VScroll.ValueChanged += new EventHandler(VScroll_ValueChanged);
            }

            void VScroll_ValueChanged(object sender, EventArgs e) {
                DevExpress.XtraEditors.VScrollBar scrollBar = sender as DevExpress.XtraEditors.VScrollBar;
                ScrollEventArgs args = new ScrollEventArgs(ScrollEventType.ThumbPosition, scrollBar.Value);
                MemoEdit memo = ((MemoEdit)scrollBar.Parent);
                ScrollBarEditorsAPIHelper helper = helper1;
                if(memo == memoEdit1) 
                    helper = helper2;
                helper.VScroll.Value = scrollBar.Value;
                 MethodInfo mi = typeof(ScrollBarEditorsAPIHelper).GetMethod("UpdateOriginalScroll", BindingFlags.NonPublic | BindingFlags.Instance);
                 mi.Invoke(helper, new object[] { args, false });
            }

If you want to also synchronize horizontal scrollbars position, you should subscribe to the HScrollbar.ValueChanged event. The code should be almost the same, except for the last line:

mi.Invoke(helper, new object[] { args, true });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜