Hi all,
I was wondering if it is possible to do the following:
I have a toolbar-like panel on the left of my form, on the right side of my form I have a larger panel containing nothing.
In the toolbar are panels containing labels with text.
I want to be able to drag such a panel to the panel on the right side and drop it there. I want it to be dropped on the exact location where I released the mouse button, that exact x-y position.
Is this at all possible?
Any help would be appreciated. |
| Tovdb Wednesday, August 30, 2006 12:34 PM |
Yes it is. Here is some sample code (I assume you understand the basics of DnD).
You need to start a DnD operation when the mouse is depressed on your panel (I'm going to use labels but it works the same).
private void label1_MouseDown ( object sender, MouseEventArgs e ) { //Get the data to pass in the DnD operation string strText = ((Label)sender).Text;
//Star the DnD operation DoDragDrop(strText, DragDropEffects.Copy); }
Next you need to have the target panel (pnlTarget) notify Windows that it can accept the DnD operation. In my example I simply require that the DnD operation contain data I can treat as text.
private void pnlTarget_DragEnter ( object sender, DragEventArgs e ) { if (e.Data.GetDataPresent("Text")) e.Effect = DragDropEffects.Copy; else e.Effect = DragDropEffects.None; }
Finally the panel needs to handle the actual drop request if it occurs. There are two tricky parts here: getting the data and getting the mouse coordinates. The mouse coordinates will be in screen coords so we have to convert to cilent coords (for the panel).
private void pnlTarget_DragDrop ( object sender, DragEventArgs e ) { //Get the text string strText = e.Data.GetData("Text", true) as string;
//Get the position relative to the client where the drop occurred Point pt = pnlTarget.PointToClient(new Point(e.X, e.Y));
//Create a new label to store in the panel Label lbl = new Label(); lbl.Text = strText; lbl.Location = pt;
//Add to the panel pnlTarget.Controls.Add(lbl); }
In this case I am simply creating a new label with the same text and adding it to the panel. You can do whatever you want. To retrieve the drop data you'll need to use the GetData method. You must specify the format of the data you want and, if it is available, it'll be returned to you. Finally be aware that the above code was contained in the owning form's class so if you create a custom panel class to handle the DnD operation then the code will change slightly. Also be aware that no error checking or cancellation support is provided so you'll want to research that as well.
Michael Taylor - 8/30/06
|
| TaylorMichaelL Wednesday, August 30, 2006 1:30 PM |
You can't give text boxes and rich text boxes a transparent background as this would make it terribly hard to use. In fact the control will throw an exception if you try.
Michael Taylor - 9/21/06 |
| TaylorMichaelL Thursday, September 21, 2006 5:38 PM |
Yes it is. Here is some sample code (I assume you understand the basics of DnD).
You need to start a DnD operation when the mouse is depressed on your panel (I'm going to use labels but it works the same).
private void label1_MouseDown ( object sender, MouseEventArgs e ) { //Get the data to pass in the DnD operation string strText = ((Label)sender).Text;
//Star the DnD operation DoDragDrop(strText, DragDropEffects.Copy); }
Next you need to have the target panel (pnlTarget) notify Windows that it can accept the DnD operation. In my example I simply require that the DnD operation contain data I can treat as text.
private void pnlTarget_DragEnter ( object sender, DragEventArgs e ) { if (e.Data.GetDataPresent("Text")) e.Effect = DragDropEffects.Copy; else e.Effect = DragDropEffects.None; }
Finally the panel needs to handle the actual drop request if it occurs. There are two tricky parts here: getting the data and getting the mouse coordinates. The mouse coordinates will be in screen coords so we have to convert to cilent coords (for the panel).
private void pnlTarget_DragDrop ( object sender, DragEventArgs e ) { //Get the text string strText = e.Data.GetData("Text", true) as string;
//Get the position relative to the client where the drop occurred Point pt = pnlTarget.PointToClient(new Point(e.X, e.Y));
//Create a new label to store in the panel Label lbl = new Label(); lbl.Text = strText; lbl.Location = pt;
//Add to the panel pnlTarget.Controls.Add(lbl); }
In this case I am simply creating a new label with the same text and adding it to the panel. You can do whatever you want. To retrieve the drop data you'll need to use the GetData method. You must specify the format of the data you want and, if it is available, it'll be returned to you. Finally be aware that the above code was contained in the owning form's class so if you create a custom panel class to handle the DnD operation then the code will change slightly. Also be aware that no error checking or cancellation support is provided so you'll want to research that as well.
Michael Taylor - 8/30/06
|
| TaylorMichaelL Wednesday, August 30, 2006 1:30 PM |
I'm going to try this now, thanks alot :) |
| Tovdb Wednesday, August 30, 2006 1:33 PM |
Works like a charm.. thanks |
| Tovdb Wednesday, August 30, 2006 2:23 PM |
This "GetData" methode, does it also work with picturebox? |
| Tovdb Wednesday, August 30, 2006 3:19 PM |
It can work with anything. The problem you are going to have is determining what to store when dragging and what to retrieve when dropped. For a picture box you can simply store the Image property as a bitmap so you can retrieve it on the other side. You'll also need to store the type of control to create. For just a few controls this works well and the drop code will be manageable. For a large number of controls then you'll need to use a different approach.
Michael Taylor - 9/8/06
|
| TaylorMichaelL Friday, September 08, 2006 1:14 PM |
Hi again,
I am a few steps further with the application. The drag and drop part works like a charm. The problem I have now is the following.
Pnltarget, the panel where all the DnDcomponents are dropped in, has the functionality of choosing a background, which is no problem.
Now the controls that you can drop onto pnltarget, I want them to be transparent, so that I can see the backgroundimage through them, but still be able to see the text.
If I try to set the background to transparent of these panels, they just don't show anymore.
Thanks in advance, Tobias |
| Tovdb Thursday, September 21, 2006 12:49 PM |
When you set the panel's background color to transparent it will disappear as it will blend in with the parent's background. Any controls you have on the panel will continue to be displayed. For labels the text will be visible but the background will be transparent as well. If you are having problems with a specific case could you post the code so we can take a look at it?
Michael Taylor - 9/21/06 |
| TaylorMichaelL Thursday, September 21, 2006 1:51 PM |
I have a large panel called pnlTarget, this contains smaller panels which in their turn contain richtextboxes or listviews. I tried to give the richtextboxes or |
| Tovdb Thursday, September 21, 2006 2:11 PM |
You can't give text boxes and rich text boxes a transparent background as this would make it terribly hard to use. In fact the control will throw an exception if you try.
Michael Taylor - 9/21/06 |
| TaylorMichaelL Thursday, September 21, 2006 5:38 PM |
hi,
did the same code of drag n drop work out 4 u.
coz i copy pasted the entire thing but it didnt work out 4 me. can u plz help me out plz, |
| rekhavenkat Friday, March 09, 2007 12:04 PM |