Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow user to drag a dynamically created control at the location of his choice

I am creating an application where I need to generate dynamically created controls say textbox or label etc.

Now what I that user can relocate that textbox to his desired location. Like we do in Visual Studio. One way is to get new location by getting values from him using textbox. But I want the user interface easy.

Can we have such functionality in winforms

like image 821
Shantanu Gupta Avatar asked Oct 06 '10 01:10

Shantanu Gupta


3 Answers

I have created a simple form that demonstrate how to move the control by dragging the control. The example assumes there is a button named button1 on the form attached to the relevant event handler.

private Control activeControl;
private Point previousLocation;

private void button1_Click(object sender, EventArgs e)
{
    var textbox = new TextBox();
    textbox.Location = new Point(50, 50);
    textbox.MouseDown += new MouseEventHandler(textbox_MouseDown);
    textbox.MouseMove += new MouseEventHandler(textbox_MouseMove);
    textbox.MouseUp += new MouseEventHandler(textbox_MouseUp);

    this.Controls.Add(textbox);
}

void textbox_MouseDown(object sender, MouseEventArgs e)
{
    activeControl = sender as Control;
    previousLocation = e.Location;
    Cursor = Cursors.Hand;
}

void textbox_MouseMove(object sender, MouseEventArgs e)
{
    if (activeControl == null || activeControl != sender)
        return;

    var location = activeControl.Location;
    location.Offset(e.Location.X - previousLocation.X, e.Location.Y - previousLocation.Y);
    activeControl.Location = location;
}

void textbox_MouseUp(object sender, MouseEventArgs e)
{
    activeControl = null;
    Cursor = Cursors.Default;
}
like image 111
Johann Blais Avatar answered Nov 17 '22 11:11

Johann Blais


It is the easiest way: First go to your solution name and right click. Select "Manage NuGet Packages". After a while a window opens with a search bar on top of it. Choose the "Browse option " and search DraggableControl package. The name Control.Draggable must be seen. Click on it then click install. Now you can use it's special commands for example.

  private void button1_Click(object sender, EventArgs e)
    {   Point p = new Point(20,70 * i);
        RichTextBox tb = new RichTextBox();
        tb.Location = p;
        tb.Height= 60;
        tb.Width = 100;
        tb.Font = Normal;
        ControlExtension.Draggable(tb,true);
        this.Controls.Add(tb);
        i++;

The ControlExtension.Draggable command can set it to be draggable or not. Just write the name of the object in the brackets (tb for me) and write a comma. Then write it is draggable (true) or not (false).

NOTE: Do not Forget to put a semicolon.

Hope it helps.

Link : https://www.nuget.org/packages/Control.Draggable/

like image 37
Akın Işık Avatar answered Nov 17 '22 11:11

Akın Işık


You can call DoDragDrop with a data object containing or representing the control to begin a drag&drop operation, then handle the container's DragDrop event and move the control.

If you want to see the control as it's dragged, you can either make a transparent (handle WM_NCHITTEST) form under the mouse showing the control (call DrawToBitmap), or not use drag&drop at all and instead handle mouse events and track state manually.

If you want Visual Studio-style snaplines, you can compare the control's bounds to other controls, make a set of lines to draw, and draw them in a paint event.

like image 1
SLaks Avatar answered Nov 17 '22 11:11

SLaks