By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jun 13 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Jun 13 '06 #2

P: n/a
Hi Walter

Thanks for the great reply

Worked a treat
Regards
Steve
"Walter Wang [MSFT]" <wa****@online.microsoft.com> wrote in message
news:0I*************@TK2MSFTNGXA01.phx.gbl...
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.

Jun 13 '06 #3

P: n/a
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.

Jun 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.