send MouseScroll from PictureBox to RichTextBox

Christ Kennedy 41 Reputation points
2022-02-28T12:14:18.033+00:00

I have a pictureBox that reflects the contents of a RichTextBox.

since the PictureBox's image changes when the scrolls the RichTextBox, I want the user to be able to scroll the RichTextBox while the mouse is over the PictureBox.

How can I detect a MouseScroll in the PictureBox and have the RichTextBox react to it.

sending the MouseEvent parameters of the PictureBox's ScrollEventHandler to the RichTextBox's ScrollEventHandler only tells the RTX to react to the PictureBox's scrolling but does NOT actually scroll the RTX.... so that doesn't work.

I've been doing something similar to what I need here but with a KeyPress event

public static void RtxOutput_KeyPress(object sender, KeyPressEventArgs e)
{
    if (formWords.RTX_Focused != null)
    {
        RichTextBox rtx = formWords.RTX_Focused;
        if (!rtx.Focused)
            rtx.Focus();
        SendKeys.Send(e.KeyChar.ToString());
    }
}

is there some equivalent for MouseWheel?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,232 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,636 Reputation points
    2022-02-28T14:34:26.7+00:00

    I'm not sure if I have understood, but maybe you can send WM_VSCROLL to the RTB

    I did a test on MouseWheel of a PictureBox =>

    178526-picturebox1-mousewheel.gif

        pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel);  
    

    then

        private void pictureBox1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)  
        {   
            int nNbLines = e.Delta * SystemInformation.MouseWheelScrollLines / 120;  
            if (nNbLines > 0)  
            {  
                for (int i = 0; i<nNbLines; i++)  
                    SendMessage(richTextBox1.Handle, WM_VSCROLL, (int)MakeWord(SB_LINEUP, 0), IntPtr.Zero);  
            }  
            else  
            {  
                for (int i = nNbLines; i < 0; i++)  
                    SendMessage(richTextBox1.Handle, WM_VSCROLL, (int)MakeWord(SB_LINEDOWN, 0), IntPtr.Zero);  
            }  
        }  
    

    with declarations :

        [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]  
        public static extern int SendMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);  
    
        public const int WM_VSCROLL = 0x0115;  
    
        public const int SB_LINEUP = 0;  
        public const int SB_LINELEFT = 0;  
        public const int SB_LINEDOWN = 1;  
        public const int SB_LINERIGHT = 1;  
        public const int SB_PAGEUP = 2;  
        public const int SB_PAGELEFT = 2;  
        public const int SB_PAGEDOWN = 3;  
        public const int SB_PAGERIGHT = 3;  
        public const int SB_THUMBPOSITION = 4;  
        public const int SB_THUMBTRACK = 5;  
        public const int SB_TOP = 6;  
        public const int SB_LEFT = 6;  
        public const int SB_BOTTOM = 7;  
        public const int SB_RIGHT = 7;  
        public const int SB_ENDSCROLL = 8;  
    
        public static uint MakeWord(byte low, byte high)  
        {  
            return ((uint)high << 8) | low;  
        }  
    

0 additional answers

Sort by: Most helpful