Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am trying to develop a paint like application and i paint a panel.

I use a text box to take the text from user and then drag it and drop it into a panel with creating a label but i want to see where exactly it is going on the panel with adding shadow like thing?. Can you give me some tips or some ideas about how to do that or is there a better way to do this?

I mean i just want to see the item that i am dragging while i am dragging it.
Like this(https://imgur.com/a/092QtTO).

What I have tried:

Here is the code currently i got;

C#
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    string strText = textBox1.Text;
    DoDragDrop(strText, DragDropEffects.Copy);
    mouse = new Point(e.X, e.Y);
}

private void panel1_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 = panel1.PointToClient(new Point(e.X, e.Y));

    //Create a new label to store in the panel
    Label lbl = new Label();
    lbl.Text = strText;
    lbl.BackColor = Color.Transparent;
    lbl.Location = pt;
    lbl.AutoSize = true;

    //Add to the panel
    panel1.Controls.Add(lbl);
}

private void panel1_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent("Text"))
        e.Effect = DragDropEffects.Copy;
    else
        e.Effect = DragDropEffects.None;
}


This code works fine but i want to improve it with adding that shadow like thing so i can drag the text to the position that i exactly want on the panel.
Posted
Updated 20-Aug-20 20:01pm
v8
Comments
OriginalGriff 17-Aug-20 10:29am    
What's wrong with what you have?
What does it do that you didn't expect, or not do that you did?

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
enis okatan 17-Aug-20 10:46am    
Sorry, is it more clear now?
BillWoodruff 17-Aug-20 11:25am    
"adding that shadow like thing" what does that refer to ?
enis okatan 17-Aug-20 11:28am    
Please see the picture i attached via imgur.

Sounds you are looking for DragDropEffects. Specifies the possible effects of a drag-and-drop operation.

High level:
Effects Description
Move 	The drag appears as a box along with the cursor. Data is moved to the target through the drop operation.

Copy 	The drag appears as a box with a plus sign along with the cursor. Data is copied to the target through the drop operation. 

Scroll 	The drop target is scrolling while the item is being dragged.

Link 	Data from the source of the item being dragged is linked to the target it is being dropped into.

All 	The data is moved and scrolled in the drop target 

Instead of an effect to the item you can also change the background color or other ways to highlight like:
C#
// in the DragEnter event after 
e.Effect = DragDropEffects.None;
this.Backcolor = Color.Red;// example, change it to any control you want to highlight

Entire example can be found in references below:
Reference: DragDropEffects Enum (System.Windows.Forms) | Microsoft Docs[^]

Uber reference:
Walkthrough: Perform a drag-and-drop operation - Windows Forms | Microsoft Docs[^]
Drag and Drop Using C#[^]
 
Share this answer
 
v2
Comments
enis okatan 17-Aug-20 11:21am    
I think i was not as clear as i could be before. Please check my updated question.
Sandeep Mewara 17-Aug-20 11:50am    
That's not winforms application. In web, dragging the text has that as default behaviour.

You can try effects like: https://www.codemag.com/article/0407031/Enable-Your-Windows-Forms-Applications-to-Drag-and-Drop-Data-Objects
An easy way is to create a delegate for the drag drop event. You can do this by subscribing a method that would update the label when an item is dragged and dropped. I would create a custom label type inherit from label or whatever your dragging and dropping so you can control the behavior. It would look something like the following. The below is untested and I did it notepad on my phone. So you may need to change it some to fit your purpose, but this will work.
<pre lang="c#">
public static class Program
	{
		public delegate void DragEventHandler(object src, EventArgs e)

		   public event System.Windows.Forms.DragEventHandler DragOver;


		public static void Main()
		{
			OnDragOver += DoSomeWork;
		}

		public static protected void OnDragOver(object src, EventArgs e)
		{
			if (DragOver != null)
			{
				DragOver(src, e);
			}
		}

		public void DoSomeWork(object o, EventArgs e)
		{
			label.Text = e.data.ToString;
		}
	}
 
Share this answer
 

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