Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-10
5
Medium Priority
?
4,573 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 100 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 100 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 1000 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

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

688 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