Preventing moving of a control out of its container

前端 未结 2 741
误落风尘
误落风尘 2020-12-21 19:41

This question is related to another question of mine which can be found here can be found here. I wanted to move a PictureBox within its parent container which

相关标签:
2条回答
  • 2020-12-21 19:49

    you can move the box unconditionally (no testing of the current location) and have a limitation for your new location:

    int nx = Math.Min(Math.Max(pictureBoxPackageView.Left + (e.X -start.X),0),pictureBoxPackageView.Parent.Width-pictureBoxPackageView.Width);
    int ny = Math.Min(Math.Max(pictureBoxPackageView.Top + (e.Y -start.Y),0),pictureBoxPackageView.Parent.Height-pictureBoxPackageView.Height);
    
    pictureBoxPackageView.Location = new Point(nx,ny);
    
    0 讨论(0)
  • 2020-12-21 19:56

    I think if you add the following code, it will move the item without snapping to the top as an option.

    //- MouseDownLocation.X
     //- MouseDownLocation.Y 
    
           int nx = Math.Min(Math.Max(label1.Left - MouseDownLocation.X + (e.X - start.X), 0), label1.Parent.Width - label1.Width);
           int ny = Math.Min(Math.Max(label1.Top - MouseDownLocation.Y + (e.Y - start.Y), 0), label1.Parent.Height - label1.Height);
    
    0 讨论(0)
提交回复
热议问题