How to move shortcut on winform in runtime

using this event, the shortcut just disappears, how do I do it?

    private void label4_MouseMove(object sender, MouseEventArgs e)
    {
        label4.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
    }
+3
source share
4 answers

Location label4refers to the container ( Formor parent control), the cursor position may be relative to the screen.

You need to configure the location. For example, if the container is Form, you can find its location on the screen and calculate from it the location of the cursor relative to the screen.

This is only one opportunity for a reason, but this is a lot :)

+3
source
handle these three event ...
Control actcontrol;
 Point   preloc;
 void label1_Mousedown(object sender, MouseEventArgs e)
        {

            actcontrol = sender as Control;
            preloc = e.Location;
            Cursor = Cursors.Default;


        }
        void label1_MouseMove(object sender, MouseEventArgs e)
        {
            if (actcontrol == null || actcontrol != sender)
                return;
            var location = actcontrol.Location;
            location.Offset(e.Location.X - preloc.X, e.Location.Y - preloc.Y);            
            actcontrol.Location = location;

        }
        void label1_MouseUp(object sender, MouseEventArgs e)
        {
            actcontrol = null;
            Cursor = Cursors.Default;

        }
+5
source

PointToClient(), X/Y , , .

: args mouse event:

Label1.Location = New Point(e.X, e.Y)

PS VB, #

+3

The location of the element relative to its parent. In this case, although you are using the absolute mouse position as your location.

You will need to translate the mouse position into the coordinate system of the parent element.

Use the PointToClientmethod for the parent label element.

+1
source

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


All Articles