Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / September 2006

Tip: Looking for answers? Try searching our database.

Scrolling UserControl with mouse wheel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jens.ro - 25 Aug 2006 17:54 GMT
Hello,

I have a UserControl that contains some child controls (in more detail
I programmatically add some labels arranged in a FlowLayoutPanel). When
the content of my UserControl exceeds its height, it should be
scrollable, therefore I set the AutoScroll property of my UserControl
to true. This works fine, but the mouse wheel does not scroll the area.

So I searched in some newsgroups and found the hint to overwrite the
function OnMouseWheel and handle this event. But the event is not fired
for my UserControl. The reason seems to be that the UserControl does
not gain the focus but one of the child controls has it. So I continued
searching and found another hint to overwrite the function OnMouseHover
and to call Focus() on this event. But also this event is not fired in
the UserControl. I think this is because my UserControl is completely
covered by the child controls. If I try it with an empty UserControl,
both events are fired, but not with child controls. It also works if I
set the Enabled property of the FlowPanelLayout to false but then all
labels are grey so this is also no solution.

Does anyone have an idea how I can get this problem solved? I simply
want to have a UserControl with child controls that can be scrolled
inside my UserControl with scrollbar and mouse wheel. This is not such
a complex thing, or?

Jens
Steen - 14 Sep 2006 08:55 GMT
Hi Jens,

In my search for an elegant way to implement scrolling I came across
your question. It seems like mouse wheel message is not routed to any
kind of control or layout that has the UserControl as base class.
Hovever the mouse wheel messages is routed to controls and layouts
having the Form as base class. Therefore you can get around your
problem by inserting a mouse wheel event handler in your form, on which
your control is placed. Create a mouse handler function in your user
control and call it manually from your form mouse wheel event handler.

Add an event handler in the form-class constructor:
this.MouseWheel += new MouseEventHandler(YourForm_MouseWheel);

Insert an event handler method in the form class:
void YourForm_MouseWheel(object sender, MouseEventArgs e)
{
  this.yourUserControl.ParentForm_MouseWheel(sender, e);
}

Create a method for control the scroll bar in your user control:
public void ParentForm_MouseWheel(object sender, MouseEventArgs e)
{
  int scrollPosition = this.flowLayoutPanel.VerticalScroll.Value;
  int scrollChange = e.Delta * -1;

  scrollPosition += scrollChange;

  if (scrollPosition < this.flowLayoutPanel.VerticalScroll.Minimum)
     scrollPosition = this.flowLayoutPanel.VerticalScroll.Minimum;
  else if (scrollPosition >
this.flowLayoutPanel.VerticalScroll.Maximum)
     scrollPosition = this.flowLayoutPanel.VerticalScroll.Maximum;

  this.flowLayoutPanel.VerticalScroll.Value = scrollPosition;
}

I have a feeling there is a better way of doing it, but haven't found
it yet. However this is how I have implemented for now it and it works
:-) I hope this will help you because the subject is not very well
described anywhere.

Best Regards
Steen

jens.ro skrev:

> Hello,
>
[quoted text clipped - 22 lines]
>
> Jens

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.