Scenario: In a form similar to QQ chat, you need to add a custom scroll bar vscroll. The panel holds space in the main form, and the vertical scroll bar vscroll on the right.
Problem: The VScroll and Panel.verticalscroll scroll bars have been implemented, but the mouse wheel event cannot be triggered.
Cause: Panel cannot focus, the wheel event cannot be triggered after a mouse click.
Workaround: After clicking on the panel, VScroll gets the focus and then customizes the mouse wheel event.
Code:
//Mouse Wheel Eventprotected Override voidOnMouseWheel (MouseEventArgs e) {Base. OnMouseWheel (e); if(E.delta <0) {Vscrollbar.value+=Vscrollbar.largechange; } Else{Vscrollbar.value-=Vscrollbar.largechange; } mPanel.VerticalScroll.value=Vscrollbar.value;}//VScroll get focus after clicking panelPrivate voidPanel_click (Objectsender, EventArgs e) {Vscrollbar.focus (); }
WinForm Mouse wheel Control custom scroll bar