Solved

Find the scroll position

Posted on 2012-03-18
3
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

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 86

Accepted Solution

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

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 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