[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1156
  • Last Modified:

MDI CLIENT AREA MOUSE WHEEL SCROLL

hi experts,

in vb 2005, i have a parent main form and a child form that occupies more than the client area's height so i need to scroll down to see it fully. i would like to be able to detect mouse wheel event so that i can scroll the mdi client area down as if i were scrolling it manually with the scrollbar.

i couldnt find any piece of info on the net about this...

thanks
0
newyuppie
Asked:
newyuppie
  • 2
  • 2
1 Solution
 
Bob LearnedCommented:
1) What is happening with the MDI parent scroll is that it is changing the location of the child forms.

2) Here is an example of detecting the mouse wheel, and moving the forms to simulate a scroll:

    Private Sub Form1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
        Form2.Top += Math.Sign(e.Delta) * 10

        Me.Refresh()
    End Sub

3) You look at the sign of the mouse wheel delta, and then multiply it by a step factor (10 in this case).

4) Refresh the window to update the scroll bar position.

Bob
0
 
newyuppieAuthor Commented:
ok, nice approach thanks for your answer i was beginning to fear nobody knew how to do it!

i have some comments on this answer:
1) it works fine, but it can be a little visually slow. isn't there any built in code that would be faster?
2) the forms look like they jump up and down with the wheel. how could i implement a smooth scroll using your approach?

thank you!

0
 
Bob LearnedCommented:
1) Visually slow?  Built-in code?  Haven't found it yet.

2) If you use the scroll bars from the MDI parent, how does look when scrolling?

3) I only figured out yesterday what MDI forms are actually doing to scroll child forms.

Bob

0
 
newyuppieAuthor Commented:
good to know im not the only one up in the middle of the night...
thanks for your example code, i just adapted it a little bit and it is working fine. in case anyone needs something similar, here's my messy code (sorry, virtually not commented, but as there is currently no info on this anywhere on the net it may serve you as well). Please if anybody designs a better approach post it in this question so we can have a source of reference for this issue.

Private Structure scrollInfo
        Private _direction As Integer
        ''' <summary>
        ''' Gets scrolling direction
        ''' </summary>
        ''' <value>Scrolling direction</value>
        ''' <returns>Direction of scroll "UP" or "DOWN"</returns>
        Friend ReadOnly Property Direction() As String
            Get
                If _direction < 0 Then
                    Return "DOWN"
                Else
                    Return "UP"
                End If
            End Get
        End Property
        ''' <summary>
        ''' Sets scrolling direction
        ''' </summary>
        ''' <value>Scrolling direction</value>
        Friend WriteOnly Property SetDirection() As Integer
            Set(ByVal value As Integer)
                _direction = value
            End Set
        End Property

    End Structure

    Private Sub FormMain_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
        'scrolling factor
        Dim ScrFactor As Integer = 80
        Dim frm As Form
        Dim scrollInfo As scrollInfo
        Dim _Has2ScrollDown As Boolean = False
        Dim _Has2ScrollUp As Boolean = False

        scrollInfo.SetDirection = Math.Sign(e.Delta)

        For Each frm In Me.MdiChildren
            If Not _Has2ScrollUp Then
                _Has2ScrollUp = frm.Top < 0
            End If
            If Not _Has2ScrollDown Then
                _Has2ScrollDown = Me.ScrollToControl(frm).Y < 0
            End If
        Next

        For Each frm In Me.MdiChildren
            If scrollInfo.Direction = "UP" Then
                If _Has2ScrollUp Then
                    frm.Top += Math.Sign(e.Delta) * ScrFactor
                End If
            ElseIf scrollInfo.Direction = "DOWN" Then
                If _Has2ScrollDown Then
                    frm.Top += Math.Sign(e.Delta) * ScrFactor
                End If
            End If
        Next

        'Me.Refresh()

    End Sub

0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now