How can I sync the scrolling of two multiline textboxes?

后端 未结 5 472
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 07:57

How can I sync the scrolling of two multiline textboxes in C# (WinForms)?

When you scroll up/down a line in TextBox A, TextBox B should scroll up/down too. The same

5条回答
  •  情话喂你
    2020-11-30 08:32

    Here is what finally helped me to fix synchronization of multiple textboxes using mouse wheel.

    I based it on very helpful Hans example.

    int WM_MOUSEWHEEL   = 0x20a; // or 522
    int WM_VSCROLL      = 0x115; // or 277
    
    protected override void WndProc(ref Message m)
    {
            //Trap WM_VSCROLL and WM_MOUSEWHEEL message and pass to buddy
            if (Buddies != null)
            {
    
                if (m.Msg == WM_MOUSEWHEEL)  //mouse wheel 
                {
    
                    if ((int)m.WParam < 0)  //mouse wheel scrolls down
                        SendMessage(this.Handle, (int)0x0115, new IntPtr(1), new IntPtr(0)); //WParam: 1- scroll down, 0- scroll up
                    else if ((int)m.WParam > 0)
                        SendMessage(this.Handle, (int)0x0115, new IntPtr(0), new IntPtr(0));
    
    
    
                    return; //prevent base.WndProc() from messing synchronization up 
                }
                else if (m.Msg == WM_VSCROLL)
                {
                    foreach (Control ctr in Buddies)
                    {
                        if (ctr != this && !scrolling && ctr != null && ctr.IsHandleCreated)
                        {
                            scrolling = true;
                            SendMessage(ctr.Handle, m.Msg, m.WParam, m.LParam);
                            scrolling = false;
                        }
    
                    }
    
                }
            }
    
        //do the usual
        base.WndProc(ref m);
    }
    

提交回复
热议问题