Search code examples
c#.netwinformsmathcollision-detection

Table layout panel cell bounds and control collision


I've just finished building a portion of a program which deals with a UI in a table layout panel. This has worked so far but i've noticed that, before i could try my controls (functionality i added) at runtime around the form, but now it is in cells, they can't be moved outside of their container cell. HOWEVER, this is great and exactly what i needed, but im finding that the controls (for example a button) will be correctly contained in the cell on the left, and top boundaries of the cell, but the bottom and right boundaries allow the control to disappear off of it. Heres some screenshots to demonstrate:

enter image description here

Here we see that the button control cannot move past the top and left bounds of the cell.

enter image description here

However here it seems to be able to move past the bottom and right bounds of the cell.

Looking back at how i allow my controls to move, i came across a section where i had set up some variables, shown below:

public static void MouseMove(object sender, MouseEventArgs e)
         {
             Control control = sender as Control;
             Control container = sender as Control;
             if (control != null)
             {
                 if (Dragging)
                 {
                     if (direction != Direction.Vertical)
                     {
                         container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
                     }
                     if (direction != Direction.Horizontal)
                     {
                         container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
                     }
                 }
             }
         }

I figured here i'm not setting a bottom and right container bounds, which would make sense, however upon exploring the intelisense, i can't seem to get container.right and container.bottom as they come with the following tooltip:

"gets the distance, in pixels, between the right edge of the control, and the left edge of it's container's client area"

and the bottom does the same, only for the bottom of the control and top of the container area.

Is there away around this? perhaps an option somewhere which connects the bottom of the control to the bottom bound of the cell, and the same for the right?

edit 1: alternatively perhaps i need to alter my mousemove event to handle collision better, so if anyone has any ideas on this too, that'd be great, i've not really looked at much collision detection before, especially in winforms.


Solution

  • Control.right is a read only property. Try setting

    if (direction != Direction.Vertical)
    {
        container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
        container.Left = Math.Min(container.Left, container.Parent.Width - container.Width;
    }
    if (direction != Direction.Horizontal)
    {
        container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
        container.Top = Math.Min(container.Top, container.Parent.Height - container.Height;
    }