Drag and Drop a Text in C#

Drag and Drop Function is when you grab an object and drag it to a different location. In C#, drag and drop is part of the standard, and any controls can be draggable. So, in this tutorial I will teach you how to create a program that has a drag and drop function. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Drag and Drop. 2. Next, add only one TextBox named TextBox1 and ListBox named ListBox1. Change the AllowDrop property of the target to True.You must design your interface like this: design 3. Now put this code for your code module.
  1. public void TextBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  2. {
  3. //Enable to Drag and Drag and when clicking it will copy directly
  4. TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy);
  5. }
  6.  
  7. public void ListBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
  8. {
  9. //Drag into the ListBox
  10. e.Effect = DragDropEffects.Copy;
  11. }
  12.  
  13. public void ListBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
  14. {
  15. //The dragged item in the ListBox will be added into the ListBox1 that has
  16. ListBox1.Items.Add(e.Data.GetData(DataFormats.Text).ToString());
  17. TextBox1.Clear();
  18. }
We use first the MouseDown Event of our TextBox1. MouseDown Event occurs when the mouse pointer is over the control and a mouse button is pressed. Then the textbox property call the DoDragDrop method. This method has two parameters. The first is the data that will be dragged (this refers to the contents of the Textbox because we will drag this into the ListBox) and the second sets the value of an enumeration, DragDropEffects, that tells the source what kind of drag and drop will be done. We also call the event DragEnter for ListBox. DragEnter event occurs when an object is dragged into the control's bounds. Usually in the DragEnter event to set the target Effect property of the e parameter. Next, we also call the event DragDrop for ListBox. DragDrop occurs when a drag-and-drop operation is completed. This will complete the drag and drop operation in the DragDrop event of the target. Output: outputdesign For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment