How to allow a user to move a control on a form

I have a winform on which I want to allow the user to move the control.

The control (currently) is a vertical line: label with border and width 1.

The context is not very important, but I will give it to you anyway. I have a background with some graphs, and I would like the user to be able to slide on the graph above the graph. Graphics done using the NPlots library. It looks something like this: http://www.ibme.de/pictures/xtm-window-graphic-ramp-signals.png

If I can find out how the user can click and drag the label / line control around the screen, I can solve my problem with the manual. Please, help.

+6
c # winforms
source share
5 answers

The code for this may be a little complicated, but essentially you will need to capture the MouseDown, MouseMove, and MouseUp events in your form. Something like that:

public void Form1_MouseDown(object sender, MouseEventArgs e) { if(e.Button != MouseButton.Left) return; // Might want to pad these values a bit if the line is only 1px, // might be hard for the user to hit directly if(eY == myControl.Top) { if(eX >= myControl.Left && eX <= myControl.Left + myControl.Width) { _capturingMoves = true; return; } } _capturingMoves = false; } public void Form1_MouseMove(object sender, MouseEventArgs e) { if(!_capturingMoves) return; // Calculate the delta and move the line here } public void Form1_MouseUp(object sender, MouseEventArgs e) { if(_capturingMoves) { _capturingMoves = false; // Do any final placement } } 
+8
source share

In WinForms, you can handle the MouseDown, MouseMove, and MouseUp events of a control. In MouseDown, specify the bit or link in which your form indicates what the mouse was clicked on, and grab the X and Y of the mouse from MouseEventArgs. On MouseMove, if a control is installed, adjust its X and Y to the difference between the last captured X and Y and the current coordinates. In MouseUp, release the control.

I would set the editing mode for this; when the user enters this mode, the current event handlers of your form controls should be disconnected and the movement handlers attached. If you want to save or discard these changes (for example, you create a custom form designer that your client can use to customize window layouts), you will also need to take some kind of picture before and after the layout controls.

+3
source share

I wrote a component to do just that: move the control on the form (or move the borderless form to the screen). You can even use it with a designer without writing code.

http://www.thomaslevesque.com/2009/05/06/windows-forms-automatically-drag-and-drop-controls-dragmove/

+2
source share

This uses an extension method that you can use for any control. It uses Rx and is based on a brief introduction to reactive extensions for .NET, Rx and a sample of Wes Dyer.

 public static class FormExtensions { public static void EnableDragging(this Control c) { // Long way, but strongly typed. var downs = from down in Observable.FromEvent<MouseEventHandler, MouseEventArgs>( eh => new MouseEventHandler(eh), eh => c.MouseDown += eh, eh => c.MouseDown -= eh) select new { down.EventArgs.X, down.EventArgs.Y }; // Short way. var moves = from move in Observable.FromEvent<MouseEventArgs>(c, "MouseMove") select new { move.EventArgs.X, move.EventArgs.Y }; var ups = Observable.FromEvent<MouseEventArgs>(c, "MouseUp"); var drags = from down in downs from move in moves.TakeUntil(ups) select new Point { X = move.X - down.X, Y = move.Y - down.Y }; drags.Subscribe(drag => c.SetBounds(c.Location.X + drag.X, c.Location.Y + drag.Y, 0, 0, BoundsSpecified.Location)); } } 

Using:

Button button1 = new button ();

button1.EnableDragging ();

0
source share

Well, frankly, there is an easier way by which you initialize a global boolean variable called what you like, in this case isMouseClicked . At your control you want to enable drag and drop, you go to the mouse event,

Make sure this event is your audit event, not a form event.

 if (e.button == MouseButtons.left) //this is where you set the boolean to true 

Then go to the mouse move event

 if (isMouseClicked == true) //You then set your location of your control. See below: Button1.Location = new Point(MousePosition.X, MousePosition.Y); 

Hover over isMouseClicked to false ;

0
source share

All Articles