Click here to Skip to main content
15,915,164 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how you would drop and drag user control on the form through code ?

What I have tried:

Nothing. Don't know the code...
Posted
Updated 7-Jul-20 6:00am
Comments
Member 12712527 7-Jul-20 12:15pm    
I don't know wpf.....

 
Share this answer
 
Drag and drop of "controls" to a form at run time is not a simple concept - You would need to produce a "container" app to hold the controls in a form that they can be dragged, and modify your app to accept the drop. That's the easy bit, Richard's solution covers the basics of that.

After that it gets horribly complicated, because a control dropped on a form doesn't do anything useful as nothing in the app knows how to interact with it. Basically what you are looking at is rewriting the whole VS designer, with the added complication that your code is already compiled and you can't add to it while it's running.

Almost certainly, you are trying to implement something which is a bad idea, and you should probably take a step back and rethign why you think this is a good idea - we have no idea what you are trying to achieve so we can;t even begin to guess, much less provide you with code!
 
Share this answer
 
Comments
Member 12712527 7-Jul-20 12:09pm    
An user control is a control much like a button. So if you can drag a button in the form then why can't you be able to drag an user control...?
OriginalGriff 7-Jul-20 12:25pm    
Ah ... if you mean drop a UserControl on your form at design time, then you can - I do it all the time.
All you have to do is compile the UC (if it's in your current project) and it will appear in the toolbox under the "YourProjectName Components" section as a draggable item when you switch to the designer.

Your use of "programmatically" implies that you want to drop controls on a running application your wrote, and that is what Richard and I were answering. At design time, a working UC just appears in the toolbox and you use it just like a Button or TextBox.
Member 12712527 7-Jul-20 13:24pm    
The solution for the button control
public partial class Form1 : Form
{
private bool isDragging = false;
private int oldX, oldY;
private Button button;

public Form1()
{
InitializeComponent();
Text = "Drag & drop button";
Size = new Size(270, 180);

button = new Button();
button.Parent = this;
button.Cursor = Cursors.Hand;
button.Text = "Button";
button.Location = new Point(20, 20);

button.MouseDown += new MouseEventHandler(OnMouseDown);
button.MouseUp += new MouseEventHandler(OnMouseUp);
button.MouseMove += new MouseEventHandler(OnMouseMove);

CenterToScreen();
}

private void OnMouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
oldX = e.X;
oldY = e.Y;
}

private void OnMouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
button.Top = button.Top + (e.Y - oldY);
button.Left = button.Left + (e.X - oldX);
}
}

private void OnMouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
}
Member 12712527 7-Jul-20 13:30pm    
In the same way user control could be done. Let me try first....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900