Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to know if there's a way to move(Change the Location) a textbox when i click somewhere on the form during runtime...i tried this on a ListView but the Textbox is appearing in another Location....
Thanks!

What I have tried:

VB
Private Sub Lv_ChkIN_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Lv_ChkIN.MouseClick
    txt_Drag.Location = txt_Drag.PointToScreen(New Point(e.X, e.Y))
    txt_Drag.BringToFront()
End Sub
Posted
Updated 28-Dec-17 19:31pm

1 solution

PointToScreen returns Screen based coordinates: Based on the top left corner of your monitor, not the containing form which contains the TextBox. If you set the TextBox to Screen coordinates, then it will be offset on your form by the amount that the form TLHC is offset from the screen TLHC. e.X and e.Y are already relative to the Form, and need no translation to be the "right place" for your textbox:
txt_Drag.Location = New Point(e.X, e.Y)


Quote:
Thats the first code i tried...when i used that code ,the top left corner of the listview detect as the top left corner of the form..

Ah! You want it relative to the ListView! All you need to do is offset it by the TLHC of the Event sender:
VB
Dim c As Control = TryCast(sender, Control)
txt_Drag.Location = New Point(c.Location.X + e.X, c.Location.Y + e.Y)
 
Share this answer
 
v2
Comments
Member 13320940 29-Dec-17 2:02am    
Thats the first code i tried...when i used that code ,the top left corner of the listview detect as the top left corner of the form..
OriginalGriff 29-Dec-17 2:23am    
Answer updated.
Member 13320940 31-Dec-17 0:40am    
Thank you! I'll let you know!
OriginalGriff 31-Dec-17 4:14am    
You're welcome!
Member 13320940 2-Jan-18 22:37pm    
It works Perfectly! Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900