Solved

VB.net 2008 Drag and Drop

Posted on 2008-10-19
2
2,410 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
Comment Utility
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
Comment Utility
Thanks very much, again.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now