Solved

VB.net 2008 Drag and Drop

Posted on 2008-10-19
2
2,418 Views
Last Modified: 2013-11-26
Hi. I am converting the following code to drag a button into a ListView. In the MSDN code below the text is dragged from TextBox1 to TextBox2. I need to drag a large red button to a ListView, but I need it to look as though the whole button is being dragged to the ListView

Private MouseIsDown As Boolean = False

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
    ' Set a flag to show that the mouse is down.
    MouseIsDown = True
End Sub

Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
    If MouseIsDown Then
        ' Initiate dragging.
        TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
    End If
    MouseIsDown = False
End Sub

Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
    ' Check the format of the data being dropped.
    If (e.Data.GetDataPresent(DataFormats.Text)) Then
        ' Display the copy cursor.
        e.Effect = DragDropEffects.Copy
    Else
        ' Display the no-drop cursor.
        e.Effect = DragDropEffects.None
    End If
End Sub

Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
    ' Paste the text.
    TextBox2.Text = e.Data.GetData(DataFormats.Text)
End Sub
0
Comment
Question by:murbro
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22760878
You can turn OFF the default cursors using the UseDefaultCursors() Property:
http://msdn.microsoft.com/en-us/library/system.windows.forms.givefeedbackeventargs.usedefaultcursors.aspx

You can create a CUSTOM CURSOR by creating an Icon from the Image of your Button.

Here is an example using two TextBoxes as in your example above.  The cursor will change into an image of the TextBox...along with whatever is currently in it:
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.AllowDrop = True ' this is necessary to make the custom cursor appear Immediately
        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.Text, DragDropEffects.Copy)
        End If
    End Sub
 
    Private Sub TextBox1_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles TextBox1.GiveFeedback
        e.UseDefaultCursors = False
        Dim bmp As New Bitmap(TextBox1.Width, TextBox1.Height)
        TextBox1.DrawToBitmap(bmp, TextBox1.ClientRectangle)
        Cursor.Current = New Cursor(bmp.GetHicon)
    End Sub
 
    Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
        e.Effect = IIf(e.Data.GetDataPresent(DataFormats.Text), DragDropEffects.Copy, DragDropEffects.None)
    End Sub
 
    Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
        TextBox2.Text = e.Data.GetData(DataFormats.Text)
    End Sub
 
End Class

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 31507726
Thanks very much, again.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question