Free Web Site - Free Web Space and Site Hosting - Web Hosting - Internet Store and Ecommerce Solution Provider - High Speed Internet
Search the Web

How to modify the absolute position of a control when run time

VB Tips:  Form, Window


   As Visual Basic can nest controls and also create and position them at run time, you sometimes are not actually sure of the absolute position of a control, with respect to the top left of the host form.
   In most cases this is not particularly important, but if you are writing sophisticated and complicated commercial software, where everything must be better than perfect, it could be essential to know this position very accurately and at all times.

   Calculating The Absolute Top

   The routine, jmAbsoluteTop, calculates the absolute Top, by just looping until there are no more Container controls.

 

' Absolute Top Position Function
Public Function jmAbsoluteTop(ctlContl As Control) As Single
Dim wrkContl As Control ' Working Control
Dim wrkTopPos As Single ' Calculated Top Position
'
' Set Error Trap
On Error GoTo jmAbsoluteTopError
'
' Initialise Working Control
Set wrkContl = ctlContl
'
' Set Initial Top Position
wrkTopPos = 0
'
' Loop until the Container is the Parent
Do
If (wrkContl.Container.Name = ctlContl.Parent.Name) Then Exit Do
wrkTopPos = wrkTopPos + wrkContl.Top ' Calculate Top Position
Set wrkContl = wrkContl.Container ' Set Next Control
Loop
'
' Return Absolute Position
jmAbsoluteTop = wrkTopPos + ctlContl.Parent.Top
Exit Function
'
' Return a Sensible Value if an Error
jmAbsoluteTopError:
jmAbsoluteTop = ctlContl.Top + ctlContl.Parent.Top
Exit Function
End Function


Copyright (c) 1999 - 2001, robert han, all rigths are reserved.