C# Disable RichTextBox's Smooth Scrolling

In this article I’ll show you how to disable RichTextBox’s Smooth Scrolling - aka make RichTextBox scroll line by line. I know this is a problem for many developers, it was a problem for me too, so that’s why I decided to post this code snippet.

Removing Smooth Scrolling

Well, there’s no easy way to fix it and since there’s no other option, you’ll have to make a custom RichTextBox and override the WndProc() function.

Basically you need to handle all the vertical scrolling: this means check if user scrolls (WM_MOUSEWHEEL), if so, use SendMessage() to send WM_VSCROLL to the control.

Before starting, you’ll need to include this namespace:

1

usingSystem.Runtime.InteropServices;

The following line sends a message to the RichTextBox which will tell it to scroll up or down:

1
2
3
4
5

SendMessage(this.Handle,WM_VSCROLL,(IntPtr)wParam,IntPtr.Zero);//wParam (3rd parameter) can be 0 or 1
// 0 to scroll up
//1toscrolldown

Now, you must create your own control, which inherits from RichTextBox and that handles the WM_MOUSEWHEEL messages separately - it will send a WM_VSCOLL each time we scroll.

classeditedRichTextBox:RichTextBox{[DllImport("user32.dll",CharSet=CharSet.Auto)]privatestaticexternintSendMessage(IntPtrhWnd,intwMsg,IntPtrwParam,IntPtrlParam);//this message is sent to the control when we scroll using the mouse
privateconstintWM_MOUSEWHEEL=0x20A;//and this one issues the control to perform scrolling
privateconstintWM_VSCROLL=0x115;protectedoverridevoidWndProc(refMessagem){if(m.Msg==WM_MOUSEWHEEL){intscrollLines=SystemInformation.MouseWheelScrollLines;for(inti=0;i<scrollLines;i++){if((int)m.WParam>0)// when wParam is greater than 0
SendMessage(this.Handle,WM_VSCROLL,(IntPtr)0,IntPtr.Zero);// scroll up
elseSendMessage(this.Handle,WM_VSCROLL,(IntPtr)1,IntPtr.Zero);// else scroll down
}return;}base.WndProc(refm);}}