How to limit the movement of Controls inside the bounds of another
Asked Answered
D

2

1

I am creating an application in which I can move the Labels that are on a PictureBox.
The problem is that I want these to only Labels move inside the PictureBox.

Here is my code:

protected void lbl_MouseMove(object sender, MouseEventArgs e)
{
    Label lbl = sender as Label;
    try
    {
        if (lbl != null && e.Button == MouseButtons.Left)
        {
            if (m_lblLocation != new Point(0, 0))
            {
                Point newLocation = lbl.Location;
                newLocation.X = newLocation.X + e.X - m_lblLocation.X;
                newLocation.Y = newLocation.Y + e.Y - m_lblLocation.Y;
                lbl.Location = newLocation;
                this.Refresh();
            }
        }
    }
    catch(Exception ex) { }
}

protected void lbl_MouseUp(object sender, MouseEventArgs e)
{
    Label lbl = sender as Label;
    try
    {
        if (lbl != null && e.Button == MouseButtons.Left)
        {
            m_lblLocation = Point.Empty;
        }
    }
    catch(Exception ex) { }
}

protected void lbl_MouseDown(object sender, MouseEventArgs e)
{
    Label lbl = sender as Label;
    try
    {
        if (lbl != null && e.Button == MouseButtons.Left)
        {
            m_lblLocation = e.Location;
        }
    }
    catch(Exception ex) { }
}

In above code I have created some mouse events for the Labels.

Dusty answered 15/11, 2018 at 9:32 Comment(1)
just put the labels on a panel, and fill the background of the panel with the imageRame
O
2

The PictureBox control is not a container, you can't directly put another control inside it, as you would do with a Panel, a GroupBox or other controls that implement IContainerControl.
You could parent the Label (in this case), setting the Label Parent to a PictureBox handle. The Label.Bounds will then reflect the parent Bounds.
However it's not necessary: you can just calculate the position of the Label in relation to the control that contains both (Label(s) and PictureBox):

You can restrict the movements of other Label controls subscribing to the MovableLabel_MouseDown/MouseUp/MouseMove events.

An example:

bool thisLabelCanMove;
Point labelMousePosition = Point.Empty;

private void MovableLabel_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left) {
        labelMousePosition = e.Location;
        thisLabelCanMove = true;
    }
}

private void MovableLabel_MouseUp(object sender, MouseEventArgs e)
{
    thisLabelCanMove = false;
}

private void MovableLabel_MouseMove(object sender, MouseEventArgs e)
{
    if (thisLabelCanMove) {
        var label = sender as Label;

        Point newLocation = new Point(label.Left + (e.Location.X - labelMousePosition.X),
                                      label.Top + (e.Location.Y - labelMousePosition.Y));
        newLocation.X = (newLocation.X < pictureBox1.Left) ? pictureBox1.Left : newLocation.X;
        newLocation.Y = (newLocation.Y < pictureBox1.Top) ? pictureBox1.Top : newLocation.Y;
        newLocation.X = (newLocation.X + label.Width > pictureBox1.Right) ? label.Left : newLocation.X;
        newLocation.Y = (newLocation.Y + label.Height > pictureBox1.Bottom) ? label.Top : newLocation.Y;
        label.Location = newLocation;
    }
}

Label Move Inside Bounds

Osteo answered 15/11, 2018 at 10:52 Comment(0)
S
0

you need to track two things: 1. is the mouse press or not - bool IsMouseDown = false; 2. the start location of the label- Point StartPoint;

// mouse is not down
private void label1_MouseUp(object sender, MouseEventArgs e)
{
    IsMouseDown = false;
}


 //mouse is down and set the starting postion
 private void label1_MouseDown(object sender, MouseEventArgs e)
 {   
     //if left mouse button was pressed
     if (e.Button == System.Windows.Forms.MouseButtons.Left)
     {
         IsMouseDown = true;
         label1.BringToFront();
         StartPoint = e.Location;
      }
   }


    //check the label is withing the borders of the picture box
    private void label1_MouseMove(object sender, MouseEventArgs e)
    {
        if (IsMouseDown)
        {
            int left = e.X + label1.Left - StartPoint.X;
            int right = e.X + label1.Right - StartPoint.X;
            int top = e.Y + label1.Top - StartPoint.Y;
            int bottom = e.Y + label1.Bottom - StartPoint.Y;
            if (left > pictureBox1.Left && top > pictureBox1.Top && pictureBox1.Bottom >= bottom && pictureBox1.Right >= right)
            {
                label1.Left = left;
                label1.Top = top;
            }
        }
    }
Soutache answered 15/11, 2018 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.