Scrollbars

  • Thread starter Thread starter Chris Smith
  • Start date Start date
C

Chris Smith

Experienced posters,

I am trying to sync the scrolling of to windows panel controls. Can someone
point me in the right direction?

Thanks,

Chris
 
* "Chris Smith said:
I am trying to sync the scrolling of to windows panel controls. Can someone
point me in the right direction?

The panel control supports scrolling of its content if the 'AutoScroll'
property is set to 'True'. Nevertheless, an event for handling the scroll
events is missing. This event can be added to the panel easily by deriving
from the 'System.Windows.Forms.Panel' class (or any other scrollable
control) and listening for scroll messages in the 'WndProc' method. The
code below can be used as a replacement for the .NET Framework's panel
control:

\\\
Imports System
Imports System.Windows.Forms

''' <summary>
''' Extends the panel control by a <c>Scroll</c> event.
''' </summary>
Public Class ScrollPanel
Inherits Panel

''' <summary>
''' Occurs when the panel is scrolled.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">
''' A <c>ScrollEventArgs</c> that contains the event data.
''' </param>
Public Event Scroll(ByVal sender As Object, ByVal e As ScrollEventArgs)

Private Const WM_VSCROLL As Int32 = &H115
Private Const WM_HSCROLL As Int32 = &H114

''' <summary>
''' Raises the <c>Scroll</c> event.
''' </summary>
''' <param name="e">
''' A <c>ScrollEventArgs</c> that contains the event data.
''' </param>
Protected Sub OnScroll(ByVal e As ScrollEventArgs)
RaiseEvent Scroll(Me, e)
End Sub

Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_HSCROLL Then
OnScroll(New ScrollEventArgs(ScrollDirection.Horizontal))
ElseIf m.Msg = WM_VSCROLL Then
OnScroll(New ScrollEventArgs(ScrollDirection.Vertical))
End If
MyBase.WndProc(m)
End Sub
End Class

''' <summary>
''' Provides data for the <c>Scroll</c> event.
''' </summary>
Public Class ScrollEventArgs
Private m_Direction As ScrollDirection

''' <summary>
''' Creates a new instance of <c>ScrollEventArgs</c>.
''' </summary>
Public Sub New(ByVal Direction As ScrollDirection)
Me.Direction = Direction
End Sub

''' <summary>
''' Gets or sets the direction the panel has been scrolled to.
''' </summary>
''' <value>The direction the panel has been scrolled to.</value>
Public Property Direction() As ScrollDirection
Get
Return m_Direction
End Get
Set(ByVal Value As ScrollDirection)
m_Direction = Value
End Set
End Property
End Class

''' <summary>
''' Provides possible scrolling directions.
''' </summary>
Public Enum ScrollDirection

''' <summary>
''' Horizontal scrolling.
''' </summary>
Horizontal

''' <summary>
''' Vertical scrolling.
''' </summary>
Vertical
End Enum
///

'ScrollPanel' provides a 'Scroll' event that is raised when the panel is
scrolled. The event handler receives a 'ScrollEventArgs' object that gives
information about the scrolling direction.
 
Chris: I need to do the exact same thing (was just getting ready to write a
message when I saw yours!) I have two panels - when once scrolls the other
one has to be forced to scroll the same amount.

However, how did you figure how HOW MUCH to scroll the second panel? I.E.
Say your user scrolled the main panel by 50 pixels - how do you know how
much they scrolled and then scroll the other panel appropriately? This code
that Herfried gave only tells you which way they scrolled, not how much (or
am I missing something).

Tom

Chris Smith said:
Wow!

Thanks for the code. I got it to work like a champ.
 
Herfried : I need to do the exact same thing. I have two panels - when once
scrolls the other one has to be forced to scroll the same amount.

However, how did you figure how HOW MUCH to scroll the second panel? I.E.
Say your user scrolled the main panel by 50 pixels - how do you know how
much they scrolled and then scroll the other panel appropriately? This
control code seems to only tell you which way they scrolled, not how much
(or am I missing something).

Tom
 
* "Tom said:
Herfried : I need to do the exact same thing. I have two panels - when once
scrolls the other one has to be forced to scroll the same amount.

However, how did you figure how HOW MUCH to scroll the second panel? I.E.
Say your user scrolled the main panel by 50 pixels - how do you know how
much they scrolled and then scroll the other panel appropriately? This
control code seems to only tell you which way they scrolled, not how much
(or am I missing something).

I never tested that, but you can check the panel's 'AutoScrollPosition'
in the first panel's 'Scroll' event and then set the 2nd panel's
'AutoScrollPosition' accordingly.
 
Herfried: Yep, after I wrote this message I did figure out that the
AutoScrollPosition.X and .Y show the amount of scrolling that the user did.
However, it seems these parameters are READ ONLY, which means that, although
I can get the direction and amount of scroll, I can't seem to figure out how
to make the other panel scroll by that same amount.

Got any ideas? Thx.

Tom
 
* "Tom said:
Herfried: Yep, after I wrote this message I did figure out that the
AutoScrollPosition.X and .Y show the amount of scrolling that the user did.
However, it seems these parameters are READ ONLY, which means that, although
I can get the direction and amount of scroll, I can't seem to figure out how
to make the other panel scroll by that same amount.

\\\
Me.Panel1.AutoScrollPosition = Me.Panel2.AutoScrollPosition
///
 
Herfried: Nope, I just tried that, and although VB.NET now accepts the
statement Panel1.AutoScrollPosition = ScrollPanel1.AutoScrollPosition
without any errors, it never sets it. I.E. Panel1 never scrolls when I
scroll ScrollPanel1. I know ScrollPanel1 is setting the proper
AutoScrollPosition because I see it via a Debug.Writeline statement;
however, setting Panel1.AutoScrollPosition = ScrollPanel1.AutoScrollPosition
does nothing. (even tried debug writing the Panel1.AutoScrollPosition after
setting it - still shows X=0; Y=0)

Any other ideas? This seems silly that there is no way to keep two panels
scrolling syncronized.... :-(

Tom
 
Tom, try this:

Panel2.AutoScrollPosition = New
Point(-Panel1.AutoScrollPosition.X, -Panel1.AutoScrollPosition.Y)



Tom said:
Herfried: Nope, I just tried that, and although VB.NET now accepts the
statement Panel1.AutoScrollPosition = ScrollPanel1.AutoScrollPosition
without any errors, it never sets it. I.E. Panel1 never scrolls when I
scroll ScrollPanel1. I know ScrollPanel1 is setting the proper
AutoScrollPosition because I see it via a Debug.Writeline statement;
however, setting Panel1.AutoScrollPosition =
ScrollPanel1.AutoScrollPosition
does nothing. (even tried debug writing the Panel1.AutoScrollPosition
after
setting it - still shows X=0; Y=0)

Any other ideas? This seems silly that there is no way to keep two panels
scrolling syncronized.... :-(

Tom
 
Hey that did the trick! I am not sure why the minus (-) sign makes the
difference, but it does.

Now, the only other issue I have is that although I want panel1 to be
autoscrolling, I -DON'T- want the scrollbars to show up. Do you know if any
way to make a panel be autoscroll and yet have no scrollbars? I know that is
an odd request but trust me, it is what I need to do in this screwy
applications case :-)

Otherwise I'll put this in another message on the forum.

Tom

Think_Fast said:
Tom, try this:

Panel2.AutoScrollPosition = New
Point(-Panel1.AutoScrollPosition.X, -Panel1.AutoScrollPosition.Y)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top