Solved

Find the scroll position

Posted on 2012-03-18
3
363 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
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now