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