Positioning form: Location() and PointToScreen() question

  • Thread starter Thread starter Colin McGuire
  • Start date Start date
C

Colin McGuire

Thanks everyone for help in my previous post. Using the
"Point.op_Addition" from the previous post I have to now focus on the
real problem.

I need to manually position a new form so that its upper left hand
corner is positioned exactly in the middle of the button that was
pressed (Button1).

I can't seem to get the knack of Location() and PointToScreen() to
position the new form here. According to the help, location() is to do
with coordinates of the container and PointToScreen() for the screen
but I can't link them together properly.
Thank you again
Colin


Private Sub Button1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseDown
Dim b As Button = CType(sender, Button)
Dim f As New Form
f.StartPosition = FormStartPosition.Manual
f.FormBorderStyle = FormBorderStyle.None
f.Location = Point.op_Addition(b.PointToScreen(b.Location), _
New Size(b.Width \ 2, b.Height \ 2))
f.Size = New Size(80, 80)
f.BackColor = Color.Yellow
f.Show()
End Sub
 
Colin,
f.Location = Point.op_Addition(b.PointToScreen(b.Location), _
New Size(b.Width \ 2, b.Height \ 2))

b.Location is relative to the owning form, b.PointToScreen is attempting to
adjust the point relative to the button itself, not relative to the owing
form.

Try:
f.Location = Point.op_Addition(Me.PointToScreen(b.Location), _
New Size(b.Width \ 2, b.Height \ 2))

Which will adjust the location of the button (relative to the form)
relative to the screen.

Hope this helps
Jay
 
Back
Top