How do you calc the absolute location of a control?
-
Greetings! If I have a control in a split-panel, which is in a tab control, which is in a form -- how can I obtain the absolute location of the original (button) control?
Dim o As Control = LogPhotosToDbBtn Dim x As Integer = LogPhotosToDbBtn.Location.X Dim y As Integer = LogPhotosToDbBtn.Location.Y While (o.Parent.Name <> "") o = o.Parent x += o.Location.X y += o.Location.Y End While Dim p1 As Point p1.X = x p1.Y = y LogFotosForm.Show() LogFotosForm.Location = p1
X and Y are never incremented because o.Parent.Name always return "". Is there a better way (one that actually works) of trying to do this? Thanks, Karen Nooobie to OOP and VB.Net 2005 -
Greetings! If I have a control in a split-panel, which is in a tab control, which is in a form -- how can I obtain the absolute location of the original (button) control?
Dim o As Control = LogPhotosToDbBtn Dim x As Integer = LogPhotosToDbBtn.Location.X Dim y As Integer = LogPhotosToDbBtn.Location.Y While (o.Parent.Name <> "") o = o.Parent x += o.Location.X y += o.Location.Y End While Dim p1 As Point p1.X = x p1.Y = y LogFotosForm.Show() LogFotosForm.Location = p1
X and Y are never incremented because o.Parent.Name always return "". Is there a better way (one that actually works) of trying to do this? Thanks, Karen Nooobie to OOP and VB.Net 2005Have you tried stepping through this in debug mode? The line: o = o.Parent will fail when you get to the form level because the form has no parent object. That may be part of your problem ...
-
Have you tried stepping through this in debug mode? The line: o = o.Parent will fail when you get to the form level because the form has no parent object. That may be part of your problem ...
I did try to debug it, it never enters the while loop -- even tho the control is buried in several levels of parent containers. Does o.Parent.Name return the parent's container's Name property? If not, what does? Thanks, Karen Nooobie to OOP and VB.Net 2005
-
I did try to debug it, it never enters the while loop -- even tho the control is buried in several levels of parent containers. Does o.Parent.Name return the parent's container's Name property? If not, what does? Thanks, Karen Nooobie to OOP and VB.Net 2005
watagal wrote: Does o.Parent.Name return the parent's container's Name property? It should. This works in Net 2002/2003.
-
Greetings! If I have a control in a split-panel, which is in a tab control, which is in a form -- how can I obtain the absolute location of the original (button) control?
Dim o As Control = LogPhotosToDbBtn Dim x As Integer = LogPhotosToDbBtn.Location.X Dim y As Integer = LogPhotosToDbBtn.Location.Y While (o.Parent.Name <> "") o = o.Parent x += o.Location.X y += o.Location.Y End While Dim p1 As Point p1.X = x p1.Y = y LogFotosForm.Show() LogFotosForm.Location = p1
X and Y are never incremented because o.Parent.Name always return "". Is there a better way (one that actually works) of trying to do this? Thanks, Karen Nooobie to OOP and VB.Net 2005watagal wrote: Greetings! If I have a control in a split-panel, which is in a tab control, which is in a form -- how can I obtain the absolute location of the original (button) control?
See non-working code in original post
X and Y are never incremented because o.Parent.Name always return "". Is there a better way (one that actually works) of trying to do this? Ok, figured out what was happening: The first parent above my button was a split panel, while the actual split panel returns the correct Name -- the Panel1 container within the split panel container does not return a Name (I guess because you can not assign it a name). Therefore, my while loop was never entered. Here's code that does work:Dim o As Control = uxLogPhotosToDbBtn Dim x As Integer = 0 Dim y As Integer = 0 While (Not o Is Nothing) x += o.Location.X y += o.Location.Y o = o.Parent End While Dim p1 As Point p1.X = x p1.Y = y uxcLogFotosForm.Show() uxcLogFotosForm.Location = p1
Thanks again for all the help, Thanks, Karen Nooobie to OOP and VB.Net 2005 -
watagal wrote: Greetings! If I have a control in a split-panel, which is in a tab control, which is in a form -- how can I obtain the absolute location of the original (button) control?
See non-working code in original post
X and Y are never incremented because o.Parent.Name always return "". Is there a better way (one that actually works) of trying to do this? Ok, figured out what was happening: The first parent above my button was a split panel, while the actual split panel returns the correct Name -- the Panel1 container within the split panel container does not return a Name (I guess because you can not assign it a name). Therefore, my while loop was never entered. Here's code that does work:Dim o As Control = uxLogPhotosToDbBtn Dim x As Integer = 0 Dim y As Integer = 0 While (Not o Is Nothing) x += o.Location.X y += o.Location.Y o = o.Parent End While Dim p1 As Point p1.X = x p1.Y = y uxcLogFotosForm.Show() uxcLogFotosForm.Location = p1
Thanks again for all the help, Thanks, Karen Nooobie to OOP and VB.Net 2005 -
Now that I have accomplished what I wanted to do, Is there a faster better easier way to do this somewhat common task? Thanks, Karen Nooobie to OOP and VB.Net 2005
Here you go, i'll be happy to give some explanation if you want. :-D
Imports System.Runtime.InteropServices
Public NotInheritable Class Api
<StructLayout(LayoutKind.Explicit)> \_ Private Structure RECT <FieldOffset(0)> Public left As Integer <FieldOffset(4)> Public top As Integer <FieldOffset(8)> Public right As Integer <FieldOffset(12)> Public bottom As Integer End Structure <DllImport("User32.dll", CharSet:=System.Runtime.InteropServices.CharSet.Auto)> \_ Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean End Function ''' <summary>Get the location of a control in screen coordinates.</summary> ''' <param name="control">The control who's location will be returned.</param> ''' <returns>Returns the location of the control in screen coordinates.</returns> Public Shared Function AbsoluteLocation(ByVal control As Control) As Point If (control Is Nothing) Then Throw New ArgumentNullException("control") Dim rect As New rect GetWindowRect(control.Handle, rect) Return New Point(rect.left, rect.top) End Function
End Class
Pompiedompiedom... ;)
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick