Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving Images by using DragDrop

Status
Not open for further replies.

michael1987

Technical User
Jan 3, 2003
1
DE
Hello
I have written a little c# application and now i have a little problem:

How can I move images from one point to another by using drag and drop?

Who can help me?
 
This little problem bedeviled me for more than a day. Part of the problem is that all of the samples I could find online dealt with just adding a new control to the base form. I wanted to add the new container control (a Label) to a Panel.

The code below is working. I'll have to do a little more playing around with it, but you can use it as a base, too.

<pre>
//in my sample app, the sender is a label with only an image on it
// so i want to contain the dropped image in a label also. it could have easily
// been any other type of new or existing container (e.g., picturebox, custom control)
private void panel2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
//create a new object to contain the data
// here i used a label because it was convenient
Label newLbl = new Label();

//add it to the list of controls contained within the form
this.panel2.Controls.Add(newLbl);

//get the image from the drop & assign it a new home
newLbl.Image = ((Image) e.Data.GetData (DataFormats.Bitmap));

//give the new container a location
newLbl.Location = this.panel2.PointToClient(new Point (e.X, e.Y));

//give the new container a size (if you don't, Microsoft may not have given
// this control a decent default size)
newLbl.Size = this.label1.Size;

//assign it a tab index if necessary
newLbl.TabIndex = 0;

//give the object some event handlers
//these handlers allow the user to drag the object around the surface of its new home
newLbl.MouseUp += new System.Windows.Forms.MouseEventHandler(this.newLbl_MouseUp);
newLbl.MouseMove += new System.Windows.Forms.MouseEventHandler(this.newLbl_MouseMove);
newLbl.MouseDown += new System.Windows.Forms.MouseEventHandler(this.newLbl_MouseDown);

}
</pre>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top