Preventing moving of a control out of its container

前端 未结 2 759
误落风尘
误落风尘 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);
    

提交回复
热议问题