Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Position form on screen 1

Status
Not open for further replies.

nelsonhoover

Programmer
Aug 15, 2007
5
0
0
US
Hi,
I'm trying to use a borderless form as a popup menu containing a treeview control. I'd like to display the form modally directly under the currently selected cell in a grid control when the cell is clicked. I've got it mostly figured out except for how to get the form positioned as desired. Any ideas on this? Thanks.

 

What code do you have so far?
Is this grid placed on the Form itself? Or is it in some other container, like a Frame?

Help us help you - show us what's you've got.

Have fun.

---- Andy
 
Form1 contains the grid control. Form2 is borderless and contains the treeview control and will be used as the popup menu to select data for the selected cell.

Code is simply:
Code:
Private Sub Grid_BtnClick(ByVal Row As Long, ByVal Col As Long)
    
    'Insert positioning code here

    Form2.Show 1 'shows Form2
    SelectedCell.Text = SelectedTreeViewItem
End Sub
All I need to know is how to position a form on screen relative to a control on another form.
I tried:
Code:
With Form2
        .Top = Form1.Top + Grid.Top
        .Left = Form1.Left + Grid.Left
        .Show 1
End With
,in an attempt to get Form2 over the grid control at least but it always shows up near the top left corner of the screen.

Thanks.
 

Change Form2's StartUpPosition to 0 - Manual in its Property.

Plus, you need to detect which cell you are clicking on and add its positions to your Top and Left values of the Form2

Have fun.

---- Andy
 
Form2 is already manual. I will add the code to detect the proper cell to align with later.
My problem is: With the above code, Form2 is near the top left corner of the screen, not anywhere close to the grid control on Form1. If I could just get Form2.Top and .Left the same as the grid control.Top and .Left, I'd be happy. I can further tweak the code myself, if I have something to start with that works.
 

I just started a new Project, 2 Forms (second Form StartUpPosition Manual, BorderStyle = 0 None)

I have just a command button in the middle of Form1 with the code:
Code:
Private Sub Command1_Click()

With Form2
    .Top = Me.Top + Command1.Top
    .Left = Me.Left + Command1.Left
    .Show vbModal
End With

End Sub
This code places Form2 (almost) at the upper left corner of Command1. Almost, because you need to detect the Height of the Form1's Title Bar and add it to the Top of Form2.Top property.

Try the same, but try it in the new Project.

Have fun.

---- Andy
 
Ok, I'll see what happens. BTW, Form1, in the previous example is actually a MDI form child and Form2 is not. Does that make a difference? Thanks alot.
 

I don't know, I do not program with MDI Parent/Childern forms.

Have fun.

---- Andy
 
Something like this should work:

Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Sub MSFlexGrid1_Click()
Dim Rec As RECT
GetWindowRect MSFlexGrid1.hwnd, Rec
Form2.Left = ScaleX(Rec.Left, vbPixels, vbTwips) + MSFlexGrid1.CellLeft
Form2.Top = ScaleY(Rec.Top, vbPixels, vbTwips) + MSFlexGrid1.CellTop
Form2.Show 1 'shows Form2
SelectedCell.Text = SelectedTreeViewItem
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top