Controls positions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: Controls positions

  1. #1
    Join Date
    Aug 2011

    Resolved Controls positions

    Hi all,

    how can I find if a control is on the Form or is created in a Frame or other container.
    I need that to know x,y coordonates.
    For example,
    Command1 Button is puted directly on the Form
    Command2 Button is puted in Frame1
    for Command1 x= Command1.x (y=blablabla)
    for Command2 x= Frame1.x + Command2.x (y=blablabla)

    I want to create a Global Function in a bas Module which send to another function this two parameters (x and y)
    Basicly I want to show the MsgBox near to control box which generates the message

    thanks for any sugestion
    Last edited by nexusm; April 20th, 2012 at 11:30 AM.

  2. #2
    Join Date
    Jul 2006

    Re: Controls positions

    You can achieve this by a small recursive function:
    Private Sub ControlPosition(Cntl As Control, ByRef XPos As Integer, ByRef YPos As Integer)
      Dim par As Object
      XPos = XPos + Cntl.Left
      YPos = YPos + Cntl.Top
      Set par = Cntl.Container
      If Not TypeOf par Is Form Then ControlPosition par, XPos, YPos
    End Sub
    Note that XPos and YPos are ByRef, so as the function (sub) can modify them. Therefore you have to set them to zero before calling the Sub.
    After calling, XPos and YPos represent the position of a control on a form, even when nested in multiple frames.
    To determine these coordinates of, say a TextBox named Text1 you go:
      Dim x%, y%
      x = 0
      y = 0
      ControlPosition Text1, x, y
    After that x and y hold the position of the control ON THE FORM where it is.
    If you want to put a messagebox in beneath that control, you have to take the position of the Form in account, too.

  3. #3
    Join Date
    Aug 2011

    Resolved Re: Controls positions

    Yeah, thanks a lot, that is it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center