Drag and Drop a Text in C#
Submitted by donbermoy on Wednesday, June 25, 2014 - 20:38.
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:
3. Now put this code for your code module.
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:
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
- public void TextBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
- {
- //Enable to Drag and Drag and when clicking it will copy directly
- TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy);
- }
- public void ListBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
- {
- //Drag into the ListBox
- e.Effect = DragDropEffects.Copy;
- }
- public void ListBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
- {
- //The dragged item in the ListBox will be added into the ListBox1 that has
- ListBox1.Items.Add(e.Data.GetData(DataFormats.Text).ToString());
- TextBox1.Clear();
- }
Add new comment
- 124 views