0

I'm trying to resize a custom control within another control (in this case the parent is called picturebox1), but it keeps going out bounds no matter which direction I resize it.

I'm trying to make this for a special paint app where the users can edit the size of multiple picture boxes on a single form.

The code for the three mouse events are listed below.

Additional Note:

I got this code from this link:

https://www.codeproject.com/Tips/709121/Move-and-Resize-Controls-on-a-Form-at-Runtime-With

Mouse Move event:

    private void MoveControl(Object sender, MouseEventArgs e)
    {

        Control control = (Control)sender;

        if (!_resizing && !_moving)
        {
            UpdateMouseEdgeProperties(control, new Point(e.X, e.Y));
            UpdateMouseCursor(control);
        }

        if (_resizing)
        {
            pictureBox1.Invalidate();
            if (MouseIsInLeftEdge)
            {
                if (MouseIsInTopEdge)
                {

                    control.Width -= (e.X - _cursorStartPoint.X);
                    control.Left += (e.X - _cursorStartPoint.X);
                    control.Height -= (e.Y - _cursorStartPoint.Y);
                  
                }
                else if (MouseIsInBottomEdge)
                {
                    control.Width -= (e.X - _cursorStartPoint.X);
                    control.Left += (e.X - _cursorStartPoint.X);
                    control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height;
                }
                else
                {
       
                    control.Width -= (e.X - _cursorStartPoint.X);
                    control.Left += (e.X - _cursorStartPoint.X);

                }
            }
            
            else if (MouseIsInRightEdge)
            {
                if (MouseIsInTopEdge)
                {
                    control.Width = (e.X - _cursorStartPoint.X) + _currentControlStartSize.Width;
                    control.Height -= (e.Y - _cursorStartPoint.Y);
                    control.Top += (e.Y - _cursorStartPoint.Y);
                }
                else if (MouseIsInBottomEdge)
                {
                    control.Width = (e.X - _cursorStartPoint.X) + _currentControlStartSize.Width;
                    control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height;
                }
                else
                {
                         control.Width -= (e.X - _cursorStartPoint.X);
                         control.Left += (e.X - _cursorStartPoint.X);
                 
                }
            }
            else if (MouseIsInTopEdge)
            {        
                 control.Height -= (e.Y - _cursorStartPoint.Y);
                 control.Top += (e.Y - _cursorStartPoint.Y);
            }
            else if (MouseIsInBottomEdge)
            {
                    control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height;
            }
            else
            {
                StopDragOrResizing(control);
            }
        }
        else if (_moving)
        {
            _moveIsInterNal = !_moveIsInterNal;
            if (!_moveIsInterNal)
            {

                int nx = Math.Min(Math.Max(control.Left + (e.X - start.X), 0), pictureBox1.Width - control.Width);
                int ny = Math.Min(Math.Max(control.Top + (e.Y - start.Y), 0), pictureBox1.Height - control.Height);

                control.Location = new Point(nx, ny);

            }
        }
    }

Mouse down event:

    private void StartMovingOrResizing(Object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
             xPos = e.X;
             yPos = e.Y;

            start = e.Location;
        }

            Control control = (Control)sender;

        if (_moving || _resizing)
        {
            return;
        }
        if (WorkType != MoveOrResize.Move &&
            (MouseIsInRightEdge || MouseIsInLeftEdge || MouseIsInTopEdge || MouseIsInBottomEdge))
        {
            _resizing = true;
            _currentControlStartSize = control.Size;
        }
        else if (WorkType != MoveOrResize.Resize)
        {
            _moving = true;
            control.Cursor = Cursors.Hand;
        }
        _cursorStartPoint = new Point(e.X, e.Y);
        control.Capture = true;
        

        DrawControlBorder(control);
        control.Invalidate();
        pictureBox1.Invalidate();

    }

MouseUp

    private void StopDragOrResizing(Object sender, MouseEventArgs e)
    {
        Control control = (Control)sender;

        _resizing = false;
        _moving = false;
        control.Capture = false;
        UpdateMouseCursor(control);

        control.Invalidate();
        DrawControlBorder(control);

    }

I tried using various projects online, but none of them had what I was seeking and the "WndProc" command was causing me problems with other parts of my project.

emoacht
  • 2,764
  • 1
  • 13
  • 24
  • The `PictureBox` is not a container like the `Panel` or `GroupBox`. Using the designer to place your UCs in a `PictureBox` doesn't make it the parent. Actually, the parent of the `PictureBox` itself is. Either make the pbox a container by changing its designer or, explicit set the `Parent` property of your UC to the pbox. You need with the latter approach to adjust the UCs locations. Find the two approaches in [this](https://stackoverflow.com/a/9387562/14171304) answer. – dr.null Mar 17 '23 at 22:24
  • Doesn't work "Dr Null". I'm NOT looking for another project where the child controls of a parent control adjust their size after the form itself resizes or via form load/ button click. I just want to be able resize the child controls WITHOUT going out of boundaries of the parent control. Nothing more. I don't care if the parent control is another picture box, panel, etc. – Bullish Programmer 117 Mar 20 '23 at 13:53
  • **I don't care if the parent control is another picture box, panel...** Will the WinForm cares. To keep moving and resizing a control at runtime within the boundaries of its parent then that parent should be a `ContainerControl`. That can't be done with a `PictureBox` unless you add the `ParentControlDesigner` attribute. Read the referred answer carefully. Also, see [this](https://stackoverflow.com/questions/2575216/how-to-move-and-resize-a-form-without-a-border) for better solutions. – dr.null Mar 20 '23 at 16:15

0 Answers0