Hi @Zero000000 ,Welcome to Q&A.
This forum only accepts English service, please modify your question and answer in English.
Regarding the Mousemove event, you need to add it manually with code.
Of course, you need to consider whether it will conflict with other controls.
I wrote you an example of dragging a usercontrol (you can also change the picturbox dock property to fill)
namespace WindowsFormsControlLibrary1
{
public partial class UserControl1 : UserControl
{
private bool isDragging = false; // whether the control is being dragged
private Point mouseOffset; // The offset between the mouse position and the control position
public UserControl1()
{
InitializeComponent();
// Subscribe to mouse down and move events
this.MouseDown += MyUserControl_MouseDown;
this.MouseMove += MyUserControl_MouseMove;
this.MouseUp += MyUserControl_MouseUp;
}
private void MyUserControl_MouseDown(object sender, MouseEventArgs e)
{
// Record the offset when the mouse is down and mark isDragging as true
mouseOffset = new Point(-e.X, -e.Y);
isDragging = true;
}
private void MyUserControl_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
// Computes the new position of the control
Point newLocation = this.Location;
newLocation.X += e.X + mouseOffset.X;
newLocation.Y += e.Y + mouseOffset.Y;
// mobile controls
this.Location = newLocation;
textBox1.Text = newLocation.X.ToString();
textBox2.Text = newLocation.Y.ToString();
}
}
private void MyUserControl_MouseUp(object sender, MouseEventArgs e)
{
// Mark isDragging to false when the mouse is up
isDragging = false;
}
}
}
This demo is the use of usercontrol in winform:
Best Regards, Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.