Solved

VB.net 2008 Drag and Drop

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…

770 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