Solved

VB.net 2008 Drag and Drop

Posted on 2008-10-19
2
2,414 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to know if my Checkbox is True in VB6.0? 9 38
SQL Exceptions 3 36
Advice in Xamarin 21 48
Need a starter for ETL protocol? 4 38
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

914 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

14 Experts available now in Live!

Get 1:1 Help Now