Drag and drop
-
Hello, I`m trying to create label and then I`m trying to move it on the form, but when I drag label I see two labels instead of one. what should I do to fix it? bool a; Label l; private void button1_Click(object sender, EventArgs e) { l = new Label(); l.Text = "text"; Controls.Add(l); l.MouseDown += new System.Windows.Forms.MouseEventHandler(mouse_down); l.MouseUp += new System.Windows.Forms.MouseEventHandler(mouse_up); l.MouseMove += new System.Windows.Forms.MouseEventHandler(mouse_move); Form1.ActiveForm.AllowDrop = true; } private void mouse_down(object sender, MouseEventArgs e) { a = true; } private void mouse_up(object sender, MouseEventArgs e) { a = false; } private void mouse_move(object sender, MouseEventArgs e) { if (a) { l.Location = e.Location; } } arnas
-
Hello, I`m trying to create label and then I`m trying to move it on the form, but when I drag label I see two labels instead of one. what should I do to fix it? bool a; Label l; private void button1_Click(object sender, EventArgs e) { l = new Label(); l.Text = "text"; Controls.Add(l); l.MouseDown += new System.Windows.Forms.MouseEventHandler(mouse_down); l.MouseUp += new System.Windows.Forms.MouseEventHandler(mouse_up); l.MouseMove += new System.Windows.Forms.MouseEventHandler(mouse_move); Form1.ActiveForm.AllowDrop = true; } private void mouse_down(object sender, MouseEventArgs e) { a = true; } private void mouse_up(object sender, MouseEventArgs e) { a = false; } private void mouse_move(object sender, MouseEventArgs e) { if (a) { l.Location = e.Location; } } arnas
You only see one Label but I think it is probably flickering very fast between two locations. Set the location this way:
l.Location = this.PointToClient(l.PointToScreen(new Point(e.X, e.Y)));
The location you get in the event arguments is relativ to the labels top left position so you have to calcualte the position you want to ste on the form. -
You only see one Label but I think it is probably flickering very fast between two locations. Set the location this way:
l.Location = this.PointToClient(l.PointToScreen(new Point(e.X, e.Y)));
The location you get in the event arguments is relativ to the labels top left position so you have to calcualte the position you want to ste on the form.Thanks;) now it work:)