Solved

Find the scroll position

Posted on 2012-03-18
3
366 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Round up to 100% in .NET 10 80
Create XML 5 46
VB.Net - ReportViewer Windows Form many reports 2 24
Get size of each directory on each mapped drive 5 20
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

12 Experts available now in Live!

Get 1:1 Help Now