12

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

Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
  • 2
    Yes, but you'll need to work hard at it. – SLaks Oct 06 '10 at 01:39
  • @SLaks: Can you direct me little bit. I can give as much time as required. I got this problem to be solved in my job but there we convinced our client for impossible. But I know it is possible So I want to do it at my home. Please let me know where to start with it. – Shantanu Gupta Oct 06 '10 at 01:46

3 Answers3

28

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;
}
Johann Blais
  • 9,389
  • 6
  • 45
  • 65
3

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/

Zujaj Misbah Khan
  • 655
  • 2
  • 11
  • 31
1

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I can see on Panel and Button Control events. They dont have DoDragDrop event/Property. Do I have to AllowDrop to true. – Shantanu Gupta Oct 06 '10 at 01:50
  • Yes; you need to set `AllowDrop` on the container. `DoDragDrop` is a method that you need to call on MouseMove shortly after MouseDown if the mouse moved outside `SystemInformation.DragSize`. – SLaks Oct 06 '10 at 01:51
  • See the example here: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop.aspx – SLaks Oct 06 '10 at 01:53
  • 1
    Thx for help, Soon I will start with posting lots of questions on stack I think, :) – Shantanu Gupta Oct 06 '10 at 01:55