C # WinForms drags controls with the mouse

I am making a From calendar in C # using WinForms. I compiled it using a two-dimensional array of panels, and inside them I have a list of <> custom controls that represent appointments.

The user should be able to drag destination controls from one panel to another (day to day).

The user control has a MouseDown and MouseUp event, which sends a message from the Parent.Parent control (custom control → day panel → calendar form) and calls the public methods StartDragging () and StopDragging (), respectively.

Inside these methods, I create a clone of the user control and add it to the form and save it in a global variable in the form called DraggedControl.

The form has an event handler for MouseMove, which looks like this:

    void Calendar_MouseMove(object sender, MouseEventArgs e)
    {
        if (DraggedControl == null)
            return;

        DraggedControl.Location = PointToClient(MousePosition);
        Refresh();
    }

However, there are two problems:

  • First of all, the user control is under everything else. I see that it is being added and removed on MouseDown and MouseUp, but it is added at 0.0 under the panels and shortcuts of the day.
  • Secondly, it does not seem to move at all with MouseMove. I have a feeling that this may be due to the fact that I move the mouse with the pressed button, and this will be a drag and drop action, not the base MouseMove.

MouseUp, , , ( , , ), .

? , , , , .

+3
1

:

DraggedControl.BringToFront();

, , MouseMove

, MouseDown, MouseMove, MouseUp, Calendar_MouseMove() . MouseMove , MouseDown.

+2

Source: https://habr.com/ru/post/1790353/


All Articles