【发布时间】:2019-10-31 09:28:06
【问题描述】:
我试过这段代码,但是当我在面板内拖动和移动标签是正常的,但是当我拖动到面板的边缘边界时它消失了我希望它在面板绑定上停止有没有办法?
private void lbl29_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
panel1.Invalidate();
}
private void lbl29_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
lbl29.Left = e.X + lbl29.Left - MouseDownLocation.X;
lbl29.Top = e.Y + lbl29.Top - MouseDownLocation.Y;
}
}
【问题讨论】:
-
您需要计算例如最大x位置,即panel.width-label.width 然后赋值x = math.min(max, x)
-
Don't move the Labels outside a PictureBox(当然,Panel 也是如此。)
-
亲爱的 Jimi 它有效,但标签必须在开始形式的面板外,但是当我将标签放在设计中的面板内时,它会从中间开始处于其他位置,我不能像它那样向上或向右右下面板只有小方块并且是不可见的下面板
-
我希望标签在开始表单的面板内
-
LabelNewLocation.X = (LabelNewLocation.X < pnl1.Left) ? pnl1.Left : LabelNewLocation.X; LabelNewLocation.Y = (LabelNewLocation.Y < pnl1.Top) ? pnl1.Top : LabelNewLocation.Y; LabelNewLocation.X = (LabelNewLocation.X + label.Width > pnl1.Right) ? label.Left : LabelNewLocation.X; LabelNewLocation.Y = (LabelNewLocation.Y + label.Height > pnl1.Bottom) ? label.Top : LabelNewLocation.Y; label.Location = LabelNewLocation;