Positioning form: Location() and PointToScreen() question

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top