Solved

Drag and Drop on controls in VB.NET - DragOver, MouseUp, etc.

Posted on 2008-06-10
5
4,551 Views
Last Modified: 2013-11-26
Hello,

I am attempting to write a program that allows the user to click on one control (a button) and drag across the form to another control (another button) and drop. I'm creating the buttons dynamically, and adding the event handlers when I create the buttons.

My question is, how do I determine which control the "drop" was on? When I mousedown on the 1st button, the sender is the 1st button, and I was storing that button's ID as ID1. Then, when I drag and drop onto button 2, I was trying to do a DragOver event and store ID2 as the sender in that event, but it doesn't always work. I've tried several events and can't seem to get the right combination.

These points are for the person that puts me in the right direction with links, etc. I will increase the points if you can give me a total solution here.

Thanks!
0
Comment
Question by:paix120
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 27

Assisted Solution

by:planocz
planocz earned 25 total points
ID: 21751186
I do not know if this will help you but here is a sample with Addhandlers on the different controls.
'FORM 1
 
Option Strict Off
Option Explicit On 
 
Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
    Inherits Form
    Private WithEvents dataGrid1 As Form1.DnDDataGrid
    Private WithEvents dataGrid2 As Form1.DnDDataGrid
    Private components As Container
    Private WithEvents dt As DataTable
    Private WithEvents dt2 As DataTable
    'Fields
    'Constructors
    'Events
    'Methods
    'Nested Types
    Public Sub New()
        MyBase.New()
        Me.dt = New DataTable("Table1")
        Me.dt2 = New DataTable("Table2")
        '
        ' Required for Windows Form Designer support
        '
        InitializeComponent()
        '
        ' TODO: Add any constructor code after InitializeComponent call
        '
        dt.Columns.Add("Int32", Type.GetType("System.String"))
        dt.Columns.Add("Char", Type.GetType("System.String"))
        Dim dr As DataRow
        Dim i As Integer
        i = 0
 
        Do While (i <= 10)
            dr = dt.NewRow
            dr("Int32") = "text " + i.ToString
            dr("Char") = ChrW(i + 33)
            dt.Rows.Add(dr)
            i = (i + 1)
 
        Loop
        dataGrid1.DataSource = dt
        dt2.Columns.Add("Int32", Type.GetType("System.String"))
        dt2.Columns.Add("Char", Type.GetType("System.String"))
        Dim dr1 As DataRow
        i = 0
 
        Do While (i <= 20)
            dr1 = dt2.NewRow
            dr1("Int32") = "row " + i.ToString
            dr1("Char") = ChrW(i + 33)
            dt2.Rows.Add(dr1)
            i = (i + 1)
 
        Loop
        dataGrid2.DataSource = dt2
    End Sub
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If (Not (components) Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Private Sub InitializeComponent()
 
        Me.dataGrid1 = New DataGridDnD.Form1.DnDDataGrid
        Me.dataGrid2 = New DataGridDnD.Form1.DnDDataGrid
        CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.dataGrid2, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        ' 
        ' dataGrid1
        ' 
        Me.dataGrid1.AllowDrop = True
        Me.dataGrid1.DataMember = ""
        Me.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dataGrid1.Location = New System.Drawing.Point(56, 24)
        Me.dataGrid1.Name = "dataGrid1"
        Me.dataGrid1.PreferredColumnWidth = 50
        Me.dataGrid1.Size = New System.Drawing.Size(208, 184)
        Me.dataGrid1.TabIndex = 0
        ' 
        ' dataGrid2
        ' 
        Me.dataGrid2.AllowDrop = True
        Me.dataGrid2.DataMember = ""
        Me.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dataGrid2.Location = New System.Drawing.Point(304, 24)
        Me.dataGrid2.Name = "dataGrid2"
        Me.dataGrid2.PreferredColumnWidth = 50
        Me.dataGrid2.Size = New System.Drawing.Size(224, 184)
        Me.dataGrid2.TabIndex = 1
        ' 
        ' Form1
        ' 
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(584, 253)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.dataGrid2, Me.dataGrid1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        AddHandler Me.Load, New System.EventHandler(AddressOf Form1_Load)
        CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.dataGrid2, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
 
    End Sub
    <STAThread()> _
    Public Shared Sub Main()
 
        Application.Run(New Form1)
 
    End Sub
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    End Sub
    Public Class DnDDataGrid
        Inherits DataGrid
        Private dndMouseDownRow As Integer
        Private dndMouseDownCol As Integer
        'Fields
        'Constructors
        'Events
        'Methods
        Public Sub New()
            MyBase.New()
            AddHandler Me.DragOver, New System.Windows.Forms.DragEventHandler(AddressOf HandleDragOver)
            AddHandler Me.DragDrop, New System.Windows.Forms.DragEventHandler(AddressOf HandleDragDrop)
            AddHandler Me.DragEnter, New System.Windows.Forms.DragEventHandler(AddressOf HandleDragEnter)
            AddHandler Me.MouseDown, New System.Windows.Forms.MouseEventHandler(AddressOf HandleMouseDown)
            AddHandler Me.MouseMove, New System.Windows.Forms.MouseEventHandler(AddressOf HandleMouseMove)
            AddHandler Me.MouseUp, New System.Windows.Forms.MouseEventHandler(AddressOf HandleMouseUp)
            Me.dndMouseDownRow = -(1)
            Me.dndMouseDownCol = -(1)
            Me.AllowDrop = True
        End Sub
        Private Sub HandleMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
            Me.dndMouseDownRow = -(1)
            Me.dndMouseDownCol = -(1)
        End Sub
        Private Sub HandleMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
            Dim ht As System.Windows.Forms.DataGrid.HitTestInfo
            ht = Me.HitTest(New Point(e.X, e.Y))
            Me.dndMouseDownRow = ht.Row
            Me.dndMouseDownCol = ht.Column
        End Sub
        Private Sub HandleMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
            Dim ht As System.Windows.Forms.DataGrid.HitTestInfo
            ht = Me.HitTest(New Point(e.X, e.Y))
            If (e.Button = MouseButtons.Left) Then
                'Console.WriteLine(ht.ToString() );
                If (((dndMouseDownRow = -(1)) _
                            OrElse (dndMouseDownCol = -(1))) _
                            OrElse ((ht.Row = dndMouseDownRow) _
                            AndAlso (ht.Column = dndMouseDownCol))) Then
                    Return
                End If
                Dim data As [String]
                data = Me(Me.dndMouseDownRow, Me.dndMouseDownCol).ToString
                'Console.WriteLine(ht.ToString() + "\n" + data);
                Me.DoDragDrop(data, DragDropEffects.Copy)
                Me.dndMouseDownRow = -(1)
                Me.dndMouseDownCol = -(1)
            End If
        End Sub
        Private Sub HandleDragOver(ByVal sender As Object, ByVal e As DragEventArgs)
            Dim ht As System.Windows.Forms.DataGrid.HitTestInfo
            ht = Me.HitTest(Me.PointToClient(New Point(e.X, e.Y)))
            'Console.WriteLine(ht.ToString());
            If (((ht.Row = Me.dndMouseDownRow) _
                        AndAlso (ht.Column = Me.dndMouseDownCol)) _
                        OrElse Not (e.Data.GetDataPresent("Text"))) Then
                e.Effect = DragDropEffects.None
            Else
                e.Effect = DragDropEffects.Copy
            End If
        End Sub
        Private Sub HandleDragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
            If e.Data.GetDataPresent("Text") Then
                e.Effect = DragDropEffects.Copy
            End If
        End Sub
        Private Sub HandleDragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
            'DataGrid dg = sender as DataGrid;
            'if(dg == null) return;
            If e.Data.GetDataPresent("Text") Then
                Dim ht As System.Windows.Forms.DataGrid.HitTestInfo
                ht = Me.HitTest(Me.PointToClient(New Point(e.X, e.Y)))
                If ((ht.Row = -(1)) _
                            OrElse (ht.Column = -(1))) Then
                    Return
                Else
                    Me(ht.Row, ht.Column) = CType(e.Data.GetData("Text"), String)
                End If
            End If
        End Sub
    End Class
End Class

Open in new window

0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 25 total points
ID: 21751292
you can use SetData to set your own drag content. See http://www.emoreau.com/Entries/Articles/2004/02/Drag-and-Drop-in-VBNet.aspx
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 21752165
Wire up the MouseMove, DragEnter and the DragDrop events of your buttons like this:

    Private Sub btn_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim btn As Button = DirectCast(sender, Button)
            Dim D As New DataObject("MyButtonData", btn)
            btn.DoDragDrop(D, DragDropEffects.All)
        End If
    End Sub

    Private Sub btn_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        If e.Data.GetDataPresent("MyButtonData") Then
            e.Effect = DragDropEffects.All
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub btn_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        Dim target As Button = DirectCast(sender, Button)
        Dim source As Button = e.Data.GetData("MyButtonData")
        Debug.Print("Source: " & source.Name)
        Debug.Print("Target: " & target.Name)
    End Sub
0
 
LVL 3

Author Comment

by:paix120
ID: 21754328
planocz - thanks, I'll take a look at that to see if I can use it. It looks helpful.

emoreau - I'm not trying to drag text, but the functions there look helpful, thanks.

Idle_Mind - looks good, I'll try it out today!

0
 
LVL 3

Author Comment

by:paix120
ID: 21756115
Idle_Mind:

Your code works for what I asked for. Thanks!

-Renee


0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

631 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