Click here to Skip to main content
15,878,543 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to make 60 label move inside of a panel i tried a code it still when user click hold and drag label inside panel it normal but when get the label out side panel the label disappear in the form

What I have tried:

private void lbl29_MouseDown(object sender, MouseEventArgs e)
           if (e.Button == System.Windows.Forms.MouseButtons.Left)
               MouseDownLocation = e.Location;


       private void lbl29_MouseMove(object sender, MouseEventArgs e)
           if (e.Button == System.Windows.Forms.MouseButtons.Left)
               lbl29.Left = e.X + lbl29.Left - MouseDownLocation.X;
               lbl29.Top = e.Y + lbl29.Top - MouseDownLocation.Y;
Updated 30-Oct-19 6:12am
Richard MacCutchan 30-Oct-19 12:37pm    
I showed you what to do in your original question. Why have you opened another one with the same issue?

I already told you how to do it, in my article that describes that very thing. I put the link in my answer to the previous post of this question.

So what if the code is written in VB.NET? The code is easily convertible to C#.

The concepts required to do this are spelled out in the article, if you bother to read it, so you can create your own controls to do this.

If you're looking for a "copy'n'paste" block of code to make this work, you're not going to find it. Doing that will just make you a copy'n'paste coder instead of someone who actually knows how to write code.
Share this answer
Member 14630006 30-Oct-19 12:15pm    
this different question dear dave and in visual basic there is handler that not exisit on c#
Dave Kreskowiak 30-Oct-19 12:22pm    
Comments are not displaying in QA right now, but from your email, NO IT IS NOT A DIFFERENT QUESTION.

The solution to the problem of the controls "disappearing" is covered in my article.

And again, the code is convertible to C#. All you're doing is creating your own control using the existing Label control to inherit from.

If you don't know how to create you own events in C#, I suggest you look it up.
Member 14630006 30-Oct-19 12:26pm    
is there StayWithinParentBounds in c#? I cant find it that why i ask if there another way
Richard MacCutchan 30-Oct-19 12:35pm    
Dave Kreskowiak 30-Oct-19 12:58pm    
No, there is no "StayWithinParentBounds" in any language or in any control. You have to code that. If you would have read the article and examined the code, you would have figured that out.
It doesn't "disappear in the form" it remains where it was - part of the Panel, just it's coordinates are outside the visible area of the panel.
When a label is part of a Panel, it is stored in the Panel.Controls collection, just as the Panel itself is stored as part of the Form.Controls collection. When you drag the label outside the bounds of the panel, that doesn't automatically change it's "parent control", it just sets an "invisible coordinate".

If you want to drag labels outside a panel, you will have to detect that it is outside the bounds, and move it from one container Controls collection to the new one. (And presumably a reverse it when you try to drag it back into the panel later).
Share this answer
Member 14630006 30-Oct-19 12:10pm    
what im trying to do is make the label move inside panel bounds

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