Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Sir/Mam I am having hard time searching over and over the net about moving a label within its panel. I already know how to move a label created in run time but the label keeps moving out in a panel. I tried this code on my own:
VB
If (label.Left + label.Width) = panel.Width Then
            
End If

If (label.Top + label.Height) = panel.Height Then
                
End If


But i am stuck and still don't know what to do. I hope you can give some advice's or ideas. Thanks in advance sir/mam.
Posted
Comments
Kschuler 7-Jan-15 10:07am    
Could you provide a specific example and explain when and why and how you are moving the label? Your situation is not clear. What code are you using to move it. The code you have now is just some IF statements that don't actually execute anything.
Kim Paulene Opulencia 7-Jan-15 10:14am    
Im going to move a label using mouse click and drag it over within a panel and not to go out the panel when moving.

Hello Kim,
This will move a label inside the panel container. It does not check or limit the travel but you should be able to figure that out. The key to the coordinate is using the Control.PointToClient method.

VB
Public Sub New()
    'This form has a Panel container Panel1 and a Label Label1
    ' This call is required by the designer.
    InitializeComponent()
End Sub
Dim _isMoving As Boolean
Dim _pt As Point
Private Sub Label1_MouseDown(sender As Object, e As MouseEventArgs) Handles Label1.MouseDown
    _isMoving = True
    _pt = New Point(e.X, e.Y)
End Sub
Private Sub Label1_MouseUp(sender As Object, e As MouseEventArgs) Handles Label1.MouseUp
    _isMoving = False
End Sub
Private Sub Label1_MouseMove(sender As Object, e As MouseEventArgs) Handles Label1.MouseMove
    Dim pos As Point = Panel1.PointToClient(Cursor.Position)
    pos.X = pos.X - _pt.X
    pos.Y = pos.Y - _pt.Y
    If _isMoving Then
        Label1.Location = pos
    End If
End Sub


Hope that helps,

regs

ron O.
 
Share this answer
 
Comments
Kim Paulene Opulencia 7-Jan-15 20:44pm    
Hi Sir thanks for your response ill try your code and ill tell you if it works on me. thanks again.
Kim Paulene Opulencia 7-Jan-15 20:53pm    
Hi sir i tried it already yeah but i have the code of moving the label with mouse drag inside the panel, what my problem is it still going out to the panel.
bojammis 7-Jan-15 21:19pm    
Make sure you have assigned the Parent of the label by either;
myform.controls.add(myLabel)
or
mylabel.parent = myform
Kim Paulene Opulencia 7-Jan-15 21:22pm    
should i put that in form load? i put it in the load but dont work.
bojammis 7-Jan-15 22:00pm    
Depends...if the label is available in the form designer - just drag the label into the panel. If you are creating the label in your code then you need to do this in the constructor after the initializeComponent() statement.

regs ron O,
You have to set the Label control to a parent container of which will be the panel. By so doing, it won't move out from the assigned parent.

Try out this code, not actually with my PC, writing from a mobile phone.

Label1.ParentContainer(Panel1)

Give it a go and make some corrections where needed!
 
Share this answer
 
Comments
bojammis 7-Jan-15 17:37pm    
I don't think there is a member ParentContainer for the Label control;
Try
Label1.Parent = Panel1
or
Panel1.Controls.Add(Label1)
Kim Paulene Opulencia 7-Jan-15 21:00pm    
i tried the code but still dont work.

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