Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find the scroll position

Posted on 2012-03-18
3
Medium Priority
?
378 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 2000 total points
ID: 37737369
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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 …
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

604 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