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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scrolling two listboxes with the MouseWheel

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
On my WinForms, I have 2 listboxes that are scrolling together using the scrollbar on either listbox.

But I can't seem to get the MouseWheel to do it correctly. Both listboxes scroll together but the listbox I am scrolling will move more lines than the other.

Using the scrollbar, I update the TopIndex on both listboxes. This works great.

What is happening is that when the MouseWheel moves, the event is fired before the original event on the first list box is processed.

For example, I have 2 listboxes (A and B). If I use my MouseWheel on listbox A, the event on listbox B is handled and then the text on listbox A is handled by the OS.

Code:
A.MouseWheel += new MouseEventHandler(A_MouseWheel);
B.MouseWheel += new MouseEventHandler(B_MouseWheel);

void A_MouseWheel(object sender, MouseEventArgs e)
{
    B.TopIndex = A.TopIndex;
}

void B_MouseWheel(object sender, MouseEventArgs e)
{
    A.TopIndex = B.TopIndex;
}

Here If I am on ListBox A and scroll with the wheel, A_MouseWheel will fire and will set B.topIndex to what A.topIndex is. At this point it is 0 since the OS hasn't moved it yet.

Then the OS moves the text and it is now 1. On the next click, B.topIndex will be changed to 1 and then the OS will move the text again on A which will now be 2.

Is there a way to get the event to fire after the original listbox has been handled?

Thanks,

Tom
 
Why aren't you handling both events in the same handler? Especially because you want them to be the same.

A.MouseWheel += new MouseEventHandler(MouseWheel);
B.MouseWheel += new MouseEventHandler(MouseWheel);

void MouseWheel(object sender, MouseEventArgs e)
{ B.TopIndex += e.ScollAmount //Something like that
A.TopIndex = B.TopIndex;}

Lodlaiden

You've got questions and source code. We want both!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top