0

I wanted to synch scrolling of ListView and TextBox, I did found how to synch two ListViews Scrolling list view when another list view is scrolled and also how to do the trick for two TextBox controls How can I sync the scrolling of two multiline textboxes?

Using tips from these topics i've got half way through, for now i've only managed to ScrollSycnhedListView scrolling to affect ScrollSycnhedTextBox scrolling but no other way around.

Ofcourse I did checked twice if Buddy value of my controls points one to another, unfortunately issue isn't that simple, here is code of my controls.

ScrollSycnhedListView

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MyApp
{
    public partial class ScrollSynchListView :ListView {

    public ScrollSynchListView() {}
    public Control Buddy { get; set; }
    private static bool scrolling;   // In case buddy tries to scroll us

        protected override void WndProc(ref Message m) {
            base.WndProc(ref m);
            // Trap WM_VSCROLL message and pass to buddy
            if ((m.Msg == 0x115 || m.Msg == 0x20a) && !scrolling && Buddy != null && Buddy.IsHandleCreated)
            {
                scrolling = true;
                SendMessage(Buddy.Handle, m.Msg, m.WParam, m.LParam);
                scrolling = false;
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    }
}

ScrollSycnhedTextBox

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MyApp
{
    public partial class ScrollSynchTextBox :TextBox {

    public ScrollSynchTextBox() {}
    public Control Buddy { get; set; }
    private static bool scrolling;   // In case buddy tries to scroll us

        protected override void WndProc(ref Message m) {
            base.WndProc(ref m);
            // Trap WM_VSCROLL message and pass to buddy
            if ((m.Msg == 0x115 || m.Msg == 0x20a) && !scrolling && Buddy != null && Buddy.IsHandleCreated)
            {
                scrolling = true;
                SendMessage(Buddy.Handle, m.Msg, m.WParam, m.LParam);
                scrolling = false;
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    }
}

I want to ask if it's possible to synch two controls of different types? What i should change in my code to get the result i want?

Community
  • 1
  • 1
MoreThanChaos
  • 2,054
  • 5
  • 20
  • 40
  • what is exactly the synchronizing behavior you want? scrolling this will scroll the other and vice-versa **regardless of** the corresponding mount of scrolling? It's important to know that because such as if your requirement is scrolling 1 item in the ListBox will scroll 1 line in the TextBox, the problem will be more complicated. – King King Dec 15 '13 at 12:27
  • @KingKing One line of textbox represents one line of listview, and the visual properties of both controls are set the way the rows align right way, so that isnt the issue here (atleast in most of cases tested by me). More tests revealed that these two controls combined act very strange, textbox seem to send feedback for mouse wheel scroll. Im afraid it just will act in that unpredictible way and i will have think about changing my approach. – MoreThanChaos Dec 15 '13 at 13:07
  • 1
    You should look at this question http://stackoverflow.com/questions/1176703/listview-onscroll-event/ for a much better understanding of what makes a ListView scroll. Ignore the accepted answer, but read the others. – Grammarian Dec 20 '13 at 03:48

0 Answers0