getting screen coordinates for positioning a floating control next to a tableLayOutPanel embeded con

S

steve

Hi All

I have textboxes within a TableLayoutpanel and I want to be able to position
an independant control adjacent to a selected textbox

This independent control allows selection of text to insert into the textbox

I am having trouble achieving this, see code below, the x position is too
far to the right and the y position is close to the bottom of the textbox
and I want it to be side by side.

I am obviously not understanding the coordinate system properly

Regards
Steve
Private Sub txtbox_Mousedown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)

'common sub for any textbox clicked

Dim mypoint As New Point

Panel1.Visible = True

Panel1.BringToFront()

mypoint.X = sender.pointtoscreen(sender.location).x + sender.width

mypoint.Y = sender.pointtoscreen(sender.location).y

Panel1.Location = mypoint

end sub
 
W

Walter Wang [MSFT]

Hi Steve,

Thank you for your post.

The coordinate system for a Windows Form is based on device coordinates,
and the basic unit of measure when drawing in Windows Forms is the device
unit (typically, the pixel). Points on the screen are described by x- and
y-coordinate pairs, with the x-coordinates increasing to the right and the
y-coordinates increasing from top to bottom. The location of the origin,
relative to the screen, will vary depending on whether you are specifying
screen or client coordinates.

A Windows Forms application specifies the position of points in a form or
control using client coordinates. The origin for client coordinates is the
upper-left corner of the client area of the control or form. Client
coordinates ensure that an application can use consistent coordinate values
while drawing in a form or control, regardless of the position of the form
or control on the screen.

In your code, since the Panel1 and the TextBox have different parent
container, the coordinates of them are not exchangable, we must convert
them to screen coordinates first before we can add or compare.

I've written following commented code to achieve the objective in your post:


Private Sub txtbox_Mousedown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)

Panel1.Visible = True
Panel1.BringToFront()

Dim txt As TextBox = CType(sender, TextBox) ' it's recommended to cast
the sender to TextBox first
Dim p as Point = txt.Parent.PointToScreen(txt.Location) ' since the
txt.Location is a client coordinate in its parent, we should call
txt.Parent.PointToScreen, not txt.PointToScreen
p.X = p.X + txt.Width
Panel1.Location = Panel1.Parent.PointToClient(p) ' again, since the
Panel1.Location is a client coordinate in its parent, we should call
Panel1.Parent.PointToClient() to convert a screen coordinate back to a
client coordinate

end sub


Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Steve,

Appreciate your update and response. I am glad to hear that the problem has
been fixed. If you have any other questions or concerns, please do not
hesitate to contact us. It is always our pleasure to be of assistance.

Have a nice day!

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Similar Threads


Top