Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to cause the mouse wheel to scroll?

Status
Not open for further replies.

Korach

Programmer
Jun 2, 2003
303
0
0
IL
I have a panel in my form where I paste pictures.
The panel is autoscroll. To react to the mouse wheel wrote this:
void PanelCtl_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
int numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines / 120;
int numberOfPixelsToMove = numberOfTextLinesToMove * 20;
}

and I want to scroll the panel according to the wheel movement.
How can I scroll the panel?
 
what calls this method? by the look of the handle it suggests that it is be the action of holding control and moving the mouse wheel.
 
This method is called when the event MouseWheel is raised and I want to write there something like:
PanelCtl.Scroll(10);
Ofcourse there is no method like that, but there has to be a way to do this.

Thanks
 
I have found the solution myself.
You don't need to write the mouse wheel, it is activated when the panel has the focus.
The panel doesn't receive the focus automatically when you press the mouse on it, so you have to add this:
Code:
private void PanelCtl_MouseEnter(object sender, System.EventArgs e)
{
     PanelCtl.Focus();
}
 
I'm working on the exact same problem and tried the above solution, but it doesn't work. Could I be calling the method wrong or something?

andrea.berman@gmail.com
 
I'm calling the code like this:

this.panelMain.MouseEnter += new System.EventHandler(this.panelMain_MouseEnter);

private void panelMain_MouseEnter(object sender, System.EventArgs e)
{
panelMain.Focus();
}

Maybe I misunderstood the solution above? In any case, I've tried a couple of different solutions at this point to get the mouse scroll to work, but no dice.

andrea.berman@gmail.com
 
Is there any scroll bar in the panel when you step into the panel?
Now I see that if the scroll bar appears after you entered the panel with the mouse, it won't work. try to put the Focus in the mouse move event.
 
There's a scrollbar there and was originally, but you can only move it manually--I can't seem to figure out how to get it to move with the mousewheel.

andrea.berman@gmail.com
 
Problem solved--I'm not sure if the code I had was right or not, but it works if you add a focus method to the MouseMove event.

andrea.berman@gmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top