Home All Groups Group Topic Archive Search About

Controlling location of form within form

Author
18 Oct 2005 7:00 PM
Tod
Here's my newbie question o' the day:

I have a form with a listbox. Once the list box is populated I want the
user to be able to click on items in the list and have more info appear
in a dialog box. I've created the form that pops ups when the user
clicks, and I've got the code the populates the form. What I want to do
is control where the form pops up. I'd like it to appear over the item
in the list the user clicked on. How do I do that?

tod

Author
18 Oct 2005 7:39 PM
Ken Halter
"Tod" <todt***@swbell.net> wrote in message
news:1129662053.756777.48970@g47g2000cwa.googlegroups.com...
> Here's my newbie question o' the day:
>
> I have a form with a listbox. Once the list box is populated I want the
> user to be able to click on items in the list and have more info appear
> in a dialog box. I've created the form that pops ups when the user
> clicks, and I've got the code the populates the form. What I want to do
> is control where the form pops up. I'd like it to appear over the item
> in the list the user clicked on. How do I do that?
>
> tod

This needs a new project that has Form1 and Form2. Place a Listbox (List1)
and a Label (Label1) on Form1 and run this....
'==============Form1 Code
Option Explicit

Private Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type

Private Declare Function GetWindowRect Lib "user32" _
   (ByVal hwnd As Long, lpRect As RECT) As Long

Private Sub Form_Load()
   Dim i As Integer

   'Stops here if IntegralHeight = False
   Debug.Assert List1.IntegralHeight

   With Label1
      .Visible = False
      .AutoSize = False
      .Caption = "-->>"
      .AutoSize = True
      .Move 0, 0
   End With

   With List1
      .Tag = "Loading" 'set a flag
      .Left = Label1.Width 'make room for the label
      Set .Font = Me.Font 'same font as form
      For i = 1 To 100
         .AddItem "Item " & i
      Next
      .Tag = ""
   End With

End Sub

Private Sub List1_Click()
   With List1
      If .Tag <> "Loading" Then
         'Show the form
         Call ShowSubForm(List1)
      End If
   End With
End Sub

Private Sub ShowSubForm(ListBx As ListBox)
   Dim tRect As RECT
   Dim iTPPX As Integer
   Dim iTPPY As Integer
   Dim fTop As Single
   Dim fTH As Single
   Dim f As Form2

   Set f = New Form2

   'Stops if the form's startup position <> manual
   Debug.Assert (f.StartUpPosition = 0)

   fTH = TextHeight("Yy")

   With ListBx

      'Pass a bit of info to the second form
      'to prove it works
      f.Caption = .List(.ListIndex)

      'Calculate the location of the selected item
      fTop = (.ListIndex - .TopIndex) * fTH

      'Show the "pointer"
      Label1.Top = .Top + fTop
      Label1.Visible = True


      '=======Get coordinates
      Call GetWindowRect(.hwnd, tRect)

   End With

   iTPPX = Screen.TwipsPerPixelX
   iTPPY = Screen.TwipsPerPixelY

   'Add one more line to fTop
   fTop = fTop + fTH

   With tRect
      .Top = (.Top * iTPPY) + fTop
      .Left = .Left * iTPPX
      .Right = .Right * iTPPX - .Left
      f.Move .Left, .Top, .Right
      f.Show vbModal
   End With

End Sub
'==============

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
18 Oct 2005 7:48 PM
Ken Halter
Show quote Hide quote
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:%23NUHquB1FHA.3780@TK2MSFTNGP12.phx.gbl...
> "Tod" <todt***@swbell.net> wrote in message
> news:1129662053.756777.48970@g47g2000cwa.googlegroups.com...
>> Here's my newbie question o' the day:
>>
>> I have a form with a listbox. Once the list box is populated I want the
>> user to be able to click on items in the list and have more info appear
>> in a dialog box. I've created the form that pops ups when the user
>> clicks, and I've got the code the populates the form. What I want to do
>> is control where the form pops up. I'd like it to appear over the item
>> in the list the user clicked on. How do I do that?
>>
>> tod

Sheesh... missed the "appear over the item" part... the code I provided
makes the form show "under" the selected item... oh well... it's all math
<g>


--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
18 Oct 2005 7:55 PM
Tom Esh
On 18 Oct 2005 12:00:53 -0700, "Tod" <todt***@swbell.net> wrote:

>Here's my newbie question o' the day:
>
>I have a form with a listbox. Once the list box is populated I want the
>user to be able to click on items in the list and have more info appear
>in a dialog box. I've created the form that pops ups when the user
>clicks, and I've got the code the populates the form. What I want to do
>is control where the form pops up. I'd like it to appear over the item
>in the list the user clicked on. How do I do that?

Alternatively you may want to consider using the mouse position
instead. It's a bit simpler to implement (only 1 Api call):

'Api declarations (bas module)...
Public Type POINTAPI
        X As Long
        Y As Long
End Type
Public Declare Function GetCursorPos Lib "user32" _
    (lpPoint As POINTAPI) As Long


'usage - Form....
Private Sub List1_Click()
    Dim ptCurs As POINTAPI

    GetCursorPos ptCurs
    FormX.Move ptCurs.X * Screen.TwipsPerPixelX, _
        ptCurs.Y * Screen.TwipsPerPixelY
End Sub



-Tom
MVP - Visual Basic
(please post replies to the newsgroup)