Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow user to move a control on the form

Tags:

c#

.net

winforms

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

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

The context is not very important but i'll give it to you anyways. I have a background with some graphics and i'd like the user to be able to slide a guideline above the graphics. The graphics are made with 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 guideline problem. Please help.

like image 801
Roast Avatar asked Sep 23 '10 18:09

Roast


People also ask

How do you access a form Control from another form?

A Form is a Control so we can cast any form in your project as a simple Control object. Example; Control c = TheForm("Form1"); Once we have this, we can gain access to ALL the child controls including the children in other container controls on the form.

How do you place Control on a form?

Add the control by drawingSelect the control by clicking on it. In your form, drag-select a region. The control will be placed to fit the size of the region you selected.

What is a user Control in Windows Forms?

A UserControl is a collection of controls placed together to be used in a certain way. For example you can place a GroupBox that contains Textbox's, Checkboxes, etc. This is useful when you have to place the same group of controls on/in multiple forms or tabs.


2 Answers

The code for this can get a bit complex, but essentially you will need to capture the MouseDown, MouseMove, and MouseUp events on your form. Something like this:

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(e.Y == myControl.Top)
    {
        if(e.X >= myControl.Left && e.X <= myControl.Left + myControl.Width)
        {
            _capturingMoves = true;
            return;
        }
    }

    _capturingMoves = false;
}

public void Form1_MouseMove(object sender, MouseEventArgs e) 
{
    if(!_capturingMoves)
        return;

    // Calculate the delta's and move the line here
}

public void Form1_MouseUp(object sender, MouseEventArgs e) 
{
    if(_capturingMoves)
    {
        _capturingMoves = false;
        // Do any final placement
    }
}
like image 133
CodingGorilla Avatar answered Sep 28 '22 01:09

CodingGorilla


In WinForms, you can handle the MouseDown, MouseMove and MouseUp events of a control. On MouseDown, set some bit or reference telling your form what control the mouse was clicked on, and capture the X and Y of the mouse from MouseEventArgs. On MouseMove, if a control is set, adjust its X and Y by the difference between your last captured X and Y and the current coordinates. On MouseUp, release the control.

I would set up an "edit mode" for this; when the user enters this mode, the current event handlers of your form's controls should be detached, and the movement handlers attached. If you want to persist or revert these changes (like you're making a custom form designer your client can use to customize window layouts), you'll also need to be able to take some sort of snapshot of the before and after layouts of the controls.

like image 42
KeithS Avatar answered Sep 28 '22 03:09

KeithS