private void usercontrol_MouseMove(object sender, MouseEventArgs e) { // Bringe das Control nach vorne wenn die Maus drüber fährt UserControlSchueler ctrl = sender as UserControlSchueler; ctrl.BringToFront(); if (aktivesUsercontrol == null || aktivesUsercontrol != sender) { return; } Point location = aktivesUsercontrol.Location; location.Offset(e.Location.X - vorherigeUserControlPos.X, e.Location.Y - vorherigeUserControlPos.Y); // Bewegen des Usercontrols auf das Panel begrenzen, sonst lässt sich das aus der Anwendung schieben if (location.X < 0) { location.X = 0; } if (location.Y < 0) { location.Y = 0; } if (location.X > (panelSchueler.Size.Width - aktivesUsercontrol.Size.Width)) { location.X = panelSchueler.Size.Width - aktivesUsercontrol.Size.Width; } if (location.Y > (panelSchueler.Size.Height - aktivesUsercontrol.Size.Height)) { location.Y = panelSchueler.Size.Height - aktivesUsercontrol.Size.Height; } aktivesUsercontrol.Location = location; }
private void usercontrol_MouseUp(object sender, MouseEventArgs e) { aktivesUsercontrol = null; Cursor = Cursors.Default; }
// https://stackoverflow.com/questions/3868941/how-to-allow-user-to-drag-a-dynamically-created-control-at-the-location-of-his-c private void usercontrol_MouseDown(object sender, MouseEventArgs e) { aktivesUsercontrol = sender as UserControlSchueler; vorherigeUserControlPos = e.Location; Cursor = Cursors.Hand; }