Solved

Find the scroll position

Posted on 2012-03-18
3
371 Views
Last Modified: 2012-06-27
Asume you have a panel that sometimes is to wide for the available visibel area. When that happens a horisontal scrollbar apears at the bottom of the panel. The panels AutoScroll property is True.

Inside the panel there is another control that needs to always position itself in the middle of the visibel area of the panel when it is being scrolled.

I have been Googling around a bit and it seams this is quite complicated.

Can someone just give me a simple example code for how to obtain the Controls new X cordinate from the panel's current scroll position.
Please also state where this code should be added.

Or can this be done by some inbuilt functions like docking? (Docking does't seam to be able to do this.)
0
Comment
Question by:daghoff
  • 2
3 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37734862
Try something like this:
Public Class Form1

    Private Sub Form1_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
        CenterControl(Panel1, Label1)
    End Sub

    Private Sub Panel1_Resize(sender As Object, e As System.EventArgs) Handles Panel1.Resize
        CenterControl(Panel1, Label1)
    End Sub

    Private Sub Panel1_Scroll(sender As System.Object, e As System.Windows.Forms.ScrollEventArgs) Handles Panel1.Scroll
        CenterControl(Panel1, Label1)
    End Sub

    Private Sub CenterControl(ByVal pnl As Panel, ByVal ctl As Control)
        If Not IsNothing(pnl) AndAlso Not IsNothing(pnl.Parent) AndAlso Not IsNothing(ctl) Then
            If pnl.IsHandleCreated AndAlso ctl.IsHandleCreated Then
                Dim CenterScreen As Point = pnl.Parent.PointToScreen(New Point(pnl.Bounds.Width / 2, pnl.Bounds.Height / 2))
                Dim CenterClient As Point = pnl.PointToClient(CenterScreen)
                CenterClient.Offset(-ctl.Width / 2, -ctl.Height / 2)
                ctl.Location = CenterClient
            End If
        End If
    End Sub

End Class

Open in new window

0
 

Author Comment

by:daghoff
ID: 37737070
Thanx Idle_Mind, it works.

But initially I only wanted this repositioning to the midle of the visibel area to occure when scrollbars have apeared in the panel. If scrollbars are not there I will position it myself.

Is there a way to detect if vertical or horisontal scrollbars has become visible in the panel?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 37737369
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question