-
Jun 24th, 2021, 06:37 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] drag and drop
Hello
I am trying to learn about drag and drop. I found a got to work dragging text from a textbox1 to a drop in a listbox1.
I am now trying to drag the backcolor of textbox1 and drop it in textbox2. Lost would be an understatement.
Here is the code
Code:
Public Class Form1
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
TextBox1.DoDragDrop(TextBox1.BackColor, DragDropEffects.Copy)
End Sub
Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
e.Effect = DragDropEffects.Copy
End Sub
Private Sub textbox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
TextBox2
End Sub
End Class
I believe the first two subs to be correct. The drop in the third sub is where I am lost.
Thank You
George
-
Jun 25th, 2021, 12:01 AM
#2
Re: drag and drop
When you do drag and drop, whatever you pass in when you call DoDragDrop, you get back from e.Data in the DragDrop event handler. I suggest that you read this thread to learn how to do that regardless of the scenario. I suggest reading the whole thread but, if you read nothing else, be sure to read post #3.
-
Jun 25th, 2021, 07:36 AM
#3
Re: drag and drop
Originally Posted by georgesutfin
Hello
I am trying to learn about drag and drop. I found a got to work dragging text from a textbox1 to a drop in a listbox1.
I am now trying to drag the backcolor of textbox1 and drop it in textbox2. Lost would be an understatement.
Here is the code
Code:
Public Class Form1
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
TextBox1.DoDragDrop(TextBox1.BackColor, DragDropEffects.Copy)
End Sub
Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
e.Effect = DragDropEffects.Copy
End Sub
Private Sub textbox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
TextBox2
End Sub
End Class
I believe the first two subs to be correct. The drop in the third sub is where I am lost.
Thank You
George
As JM told you, but here's the code...
Code:
Private Sub textbox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
If e.Data.GetDataPresent(GetType(Color)) Then
TextBox2.BackColor = DirectCast(e.Data.GetData(GetType(Color)), Color)
End If
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 25th, 2021, 08:50 AM
#4
Thread Starter
Hyperactive Member
Re: drag and drop
thank you .paul
I have a better understanding now.
Code:
Public Class Form1
Private Sub textBox1_mousedown(ByVal sender As Object, ByVal e As System.Windows.Forms. MouseEventArgs) Handles TextBox1.MouseDown
TextBox1.DoDragDrop(TextBox1.BackColor, DragDropEffects.Copy)
End Sub
Private Sub textbox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
If e.Data.GetDataPresent(GetType(Color)) Then
TextBox2.BackColor = DirectCast(e.Data.GetData(GetType(Color)), Color)
End If
End Sub
End Class
However the second sub never fires.
-
Jun 25th, 2021, 10:05 AM
#5
Re: drag and drop
If only someone had directed you to an existing thread that explains and demonstrates how the process works.
-
Jun 25th, 2021, 10:56 AM
#6
Re: drag and drop
Originally Posted by jmcilhinney
If only someone had directed you to an existing thread that explains and demonstrates how the process works.
As JM is trying to tell you, read the thread. It's probably because you need AllowDrop and you need to handle DragEnter, etc...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 26th, 2021, 12:06 PM
#7
Re: drag and drop
Here's the complete and tested working code...
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox2.AllowDrop = True
End Sub
Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
TextBox1.DoDragDrop(TextBox1.BackColor, DragDropEffects.Copy)
End If
End Sub
Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
If e.AllowedEffect = DragDropEffects.Copy Then
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub textbox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
If e.Data.GetDataPresent(GetType(Color)) Then
TextBox2.BackColor = DirectCast(e.Data.GetData(GetType(Color)), Color)
End If
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 26th, 2021, 06:19 PM
#8
Re: drag and drop
To get a smoother dragdrop effect, set AllowDrop for your form and all of your controls, then handle DragEnter for all of those too, by adding to the handles clause in TextBox2_DragEnter, which you should probably rename all_DragEnter. Your dragged cursor will show DragDropEffects.Copy on any control it passes over, including your form...
Last edited by .paul.; Jun 26th, 2021 at 11:28 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 27th, 2021, 05:26 AM
#9
Thread Starter
Hyperactive Member
Re: drag and drop
.paul thank you for the working example.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|