how make label move inside panel bounds - c#

i tried this code but when i drag and move label inside panel is normal but when i drag to edge bound of panel it disappear i want it to stop on panel bound is there a way for that?
private void lbl29_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
panel1.Invalidate();
}
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;
}
}

You can try these:
Controls.SetChildIndex(lbl29, 0); or
Controls.SetChildIndex(panel1, 1); or
lbl29.BringToFront(); or
panel1.SendToBack();
But Controls.SetChildIndex(lbl29, 0); and Controls.SetChildIndex(panel1, 1); are more useful.

Related

Check if a control is on top of another

I've been trying to make a drag and drop game. I have 4 panels and 4 labels. You have to drag the labels on top of the correct panel.
The problem is checking if a label is on top of the panel. The user can frely drag the labels.
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
button1.Left = e.X + button1.Left - MouseDownLocation.X;
button1.Top = e.Y + button1.Top - MouseDownLocation.Y;
}
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
Here is the code i used to move the control. I have to mention that this is a test project, so I used a button instead of a label, but the idea is the same.
Is there any way if I can check whether a control is on top of another or not ?
After each move, simply get the Rectangle from the Bounds property of your button and panel, then use either Intersect() or Contains():
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
button1.Location = new Point(e.X + button1.Left - MouseDownLocation.X, e.Y + button1.Top - MouseDownLocation.Y);
Rectangle btnRC = button1.Bounds;
Rectangle pnlRC = panel1.Bounds;
// see if the rectangles INTERSECT
if (pnlRC.IntersectsWith(btnRC))
{
panel1.BackColor = Color.Green;
}
else
{
panel1.BackColor = this.BackColor;
}
// see if the panel COMPLETELY CONTAINS the button
if (pnlRC.Contains(btnRC))
{
panel1.BackColor = Color.Green;
}
else
{
panel1.BackColor = this.BackColor;
}
}
}
To check if the mouse is over the control, you can check if the Cursor.Position is in the ClientRectangle of the control, but you first need to call PointToClient method of the control to convert the cursor position relative to the panel's (0,0), for example:
var b = panel1.ClientRectangle.Contains(panel1.PointToClient(Cursor.Position));
The flag can be checked in the same event handler which is used to move the control, for example:
if(b) panel1.BackColor = Color.Red; else panel1.BackColor = Color.Gray;

c# Dragging PictureBox inside Panel

I have a PictureBox1 draggable with this code:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
x = e.X;
y = e.Y;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (pictureBox1.Left + pictureBox1.Width> panel1.Width)
pictureBox1.Left += (e.X - x);
}
}
But I can't get bound restrictions,
I just want move the Picture inside a Panel, like this:
Example
Any ideas?
Thanks
Try this:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int left = 0;
if (pictureBox1.Left >= 0 && pictureBox1.Right <= panel1.Width)
left = pictureBox1.Left + (e.X - x);
left = Math.Min(panel1.Width - pictureBox1.Width , left);
left = Math.Max(0, left);
pictureBox1.Left = left;
}
}

Moving control in runtime

I'm with a problem moving a label on a panel. When I move this label, reached the top and left (0.0), the label respects the top and left. To spend half of the screen, the label exceeds the panel as shown in picture.
My codes:
public partial class frmStandard : Form
{
Point startposition;
}
public void MouseDown(object sender, MouseEventArgs e)
{
startposition = e.Location;
}
public void MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
((Label)sender).Left = Math.Max(0, e.X + ((Label)sender).Left - startposition.X);
((Label)sender).Top = Math.Max(0, e.Y + ((Label)sender).Top - startposition.Y);
}
}
I need the label does not exceed the panel size.What should be added in the codes?
You need to check the other Borders.
You will have to use the dimension data of the containing Panel for this.
In the spirit of keeping things dynamic, as your code already is, I use he Label's Parent instead of referring to just the one Panel:
private void MouseMove(object sender, MouseEventArgs e)
{
Label L = (Label)sender;
Rectangle PR = L.Parent.ClientRectangle;
if (e.Button == MouseButtons.Left)
{
L.Left = Math.Min(Math.Max(0, e.X + L.Left - startposition.X), PR.Right - L.Width);
L.Top = Math.Min( Math.Max(0, e.Y + L.Top - startposition.Y), PR.Bottom - L.Height);
}
}
To keep it even more general, one could replace Label with Control and let the user move other Controls around wit the same pieces of code..

MouseMove doesn't get the right coordinate points

I have a label I am trying to drag. I click on the label, and on the MouseMove() event I am trying to relocate the position of the label.
public void MyLabel_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
((Label)sender).Location = Cursor.Position;
// I have also tried e.Location but none of these moves the label to
// where the the cursor is, always around it, sometimes completely off
}
}
You usually need to store the offset location of the initial mouse down point in the control, or else the control will move on you in a jittering fashion. Then you just do the math:
Point labelOffset = Point.Empty;
void MyLabel_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
labelOffset = e.Location;
}
}
void MyLabel_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
Label l = sender as Label;
l.Location = new Point(l.Left + e.X - labelOffset.X,
l.Top + e.Y - labelOffset.Y);
}
}

drag and drop picturebox between forms

what is the best way to do this y need move a picturebox and drop in the other form y used this for move my picture
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
x = e.X;
y = e.Y;
}
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pictureBox2.Left += (e.X -x);
pictureBox2.Top += (e.Y - y);
}
}
The best way is to use the Drag and Drop eventing.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragdrop.aspx
A good article describing nearly exactly what I believe you're trying to do is here:
http://ondotnet.com/pub/a/dotnet/2002/11/11/dragdrop.htm
Hope that helps.

Categories