MouseWheel mouse wheel Event trigger for panel in Winform

Source: Internet
Author: User

If you set the AutoScroll property of a form or container control (such as a Panel control) to True, the scroll bar appears when the form or Panel does not contain any of its child controls, and you can display the entire contents of the form or panel by moving the scrollbar. However, by default, the scroll bar movement can only be achieved by dragging the scroll bar with the mouse, instead of moving up and down through the mouse's scroll wheel, it is necessary to manually add code to implement this function.

Scrolls the mouse wheel, triggering the MouseWheel event on the form or control. However, in vs.net2005, the list of events for forms and controls does not contain the MouseWheel event, so all code for this event must be entered manually. Here is an example of adding a panel's MouseWheel event. First, in the form's constructor or InitializeComponent function, add a subscription to the MouseWheel event:

This.  Panel.  MouseWheel+ =newSystem.  Windows.  Forms.  MouseEventHandler(this.  Panel_mousewheel);                 

This only enables the Panel to receive scrolling events for the mouse, but does not allow the scroll bar to move up or down, so you need to add the following code:

Private void Panel_mousewheel(object Sender,  Mouseeventargs E) { Span class= "Typ" >panel. Verticalscroll. Value+=10; panel. Refreshpanel. Invalidatepanel. Update               /span>                

If you add the above code, you think it's all right, that's a big mistake. Running the above program, you can see that the Panel control does not react to mouse wheel events because the Panel control does not have the focus by default, so it cannot capture the mouse and cannot capture the mouse's wheel events. The workaround is to have the Panel control focus first, which can be done through the panel's MouseClick event or MouseEnter event to perform the This.Panel.Focus () to achieve the goal. This gives the focus when the mouse clicks on the Panel control or moves to the Panel control. Therefore, you will also need to add the following code:

Privatevoidpanel_mouseclick(object sender,MouseEventArgs e){ this.  Panel.  Focus();}                

This way, you can move the scroll bar by scrolling through the mouse by running the program.

The other thing to say is that after the scrolling event that triggered the mouse, the function parameter that handles the event has a Delta attribute in MouseEventArgs E, and by default, the E is scrolled up. DELTA=120, scroll down E. delta=-120.

The above program is not particularly perfect, because when the Panel control is large and the scrollbar is not displayed, or the scroll bar is already at the top and the wheel is scrolling up, or the scroll bar is already at the bottom and the scroll wheel is scrolling downward, the Panel.refresh () is also executed. Panel.invalidate (); Panel.update (); The form redraws the code, consuming more resources. You can therefore judge the current state of the panel before executing the code. The complete program code is as follows:

Public Partial Class Plotinfoform. Form{ IntLastrightpanelverticalscrollvalue=-1;Provides a static variable for the mouse scroll event to store the Verticalscroll.value after the last scroll Public Plotinfoform() { InitializeComponent(); This.Rightpanel.MouseWheel+=Newsystem.Windows.Forms.MouseEventHandler(This.Rightpanel_mousewheel); } Activates a scrolling event when the mouse is clicked on the right panel Private voidRightpanel_mouseclick(ObjectSender, MouseEventArgsE) { This.Rightpanel.Focus(); } Private voidRightpanel_mousewheel(ObjectSender, MouseEventArgsE) { If(! (Rightpanel.Verticalscroll.Visible==False|| (Rightpanel.Verticalscroll.Value==0&&E.Delta>0)|| (Rightpanel.Verticalscroll.Value==Lastrightpanelverticalscrollvalue&&E.Delta<0))) {Rightpanelverticalscroll. Value+=10; Lastrightpanelverticalscrollvalue=rightpanel. Verticalscroll. Value; Rightpanel. Refresh  Rightpanel. Invalidate  Rightpanel. Update} }}    /span>                



Reference: Http://outofmemory.cn/code-snippet/3152/C-panel-mousewheel-shubiao-gunlun-event-trigger

MouseWheel mouse wheel Event trigger for panel in Winform

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.