CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2011
    Posts
    153

    Question Drag n' Drop between controls to switch values.

    Long time no see. I have been feeling out other languages like perl, python, French, Russian, and c sharp.

    Anyway, I have this code.

    Code:
    Imports System.Windows.Forms.Button
    
    Public Class ProBtn
        Inherits Button
    
        Private Vsfn As String 'Value string
        Private Vssn As String 'Value string
    
        Dim x As Integer
        Dim named As ProBtn
    
        Public Property FileName() As String
            Get
                Return Vsfn
            End Get
            Set(ByVal Value As String)
                Vsfn = Value
            End Set
        End Property
    
        Public Property SafeFileName() As String
            Get
                Return Vssn
            End Get
            Set(ByVal Value As String)
                Vssn = Value
            End Set
        End Property
    
        Public Sub New()
            Me.AllowDrop = True
            Me.BackgroundImage = My.Resources.gradient1edited
            Me.BackgroundImageLayout = ImageLayout.Stretch
            Me.FileName = "vacant"
            Me.SafeFileName = "vacant"
            Me.Margin = New Padding(1, 1, 1, 1)
            Me.Size = New Point(60, 60)
        End Sub
    
        Private Sub ProBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
            If Form1.remove = 1 Then
                Form1.ListBox1.Items(Form1.x).text = "vacant"
                Form1.ListBox2.Items(Form1.x).text = "vacant"
                Form1.II = Form1.II - 1
                Form1.remove = 0
            ElseIf Form1.switch = 1 Then
                Form1.Label2.Text = Me.Name
                Form1.s1a = Me.Name
                Form1.Label3.Text = Me.FileName
                Form1.s1b = Me.FileName
                Form1.Label4.Text = Me.SafeFileName
                Form1.s1c = Me.SafeFileName
                Form1.Label5.Text = Form1.x
                Form1.s1d = Form1.x
                Form1.switch = 2
            ElseIf Form1.switch = 2 Then
                Form1.SpeCon(Form1.x).FileName = Form1.s1b
                Form1.SpeCon(Form1.x).SafeFileName = Form1.s1c
                Form1.SpeCon(Form1.x).Image = Icon.ExtractAssociatedIcon(Form1.s1b).ToBitmap
    
                Me.FileName = Form1.s2b
                Me.SafeFileName = Form1.s2c
                Me.Image = Icon.ExtractAssociatedIcon(Form1.s2b).ToBitmap
    
            Else
                If Me.FileName = "vacant" Then
                    Exit Sub
                Else
                    Dim process As New Process()
                    process.StartInfo.UseShellExecute = False
                    process.StartInfo.FileName = Me.FileName
                    process.StartInfo.WorkingDirectory = IO.Path.GetDirectoryName(Me.FileName)
                    process.Start(Me.FileName)
    
                End If
            End If
            Form1.Label5.Text = Me.SafeFileName
        End Sub
    
        Private Sub ProBtn_MouseDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseDown
    
        End Sub
    
        Private Sub ProBtn_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        End Sub
    
        Public Sub ProBtn_MouseEnter() Handles Me.MouseEnter
            Me.BackgroundImage = My.Resources.gradient1
    
            Form1.x = Me.Name.Split("n").Last
    
            Form1.Label6.Text = Me.Name
            Form1.s2a = Me.Name
            Form1.Label7.Text = Me.FileName
            Form1.s2b = Me.FileName
            Form1.Label8.Text = Me.SafeFileName
            Form1.s2c = Me.SafeFileName
            Form1.Label9.Text = Form1.x
            Form1.s2d = Form1.x
        End Sub
    
        Private Sub ProBtn_MouseLeave() Handles Me.MouseLeave
            Me.BackgroundImage = My.Resources.gradient1edited
        End Sub
    
        Private Sub ProBtn_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
            e.Effect = e.AllowedEffect
    
            Form1.Label2.Text = Me.Name
            Form1.s1a = Me.Name
            Form1.Label3.Text = Me.FileName
            Form1.s1b = Me.FileName
            Form1.Label4.Text = Me.SafeFileName
            Form1.s1c = Me.SafeFileName
            Form1.Label5.Text = Form1.x
            Form1.s1d = Form1.x
        End Sub
    
        Private Sub ProBtn_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragOver
            Form1.Label6.Text = Me.Name
            Form1.Label7.Text = Me.FileName
            Form1.Label8.Text = Me.SafeFileName
            Form1.Label9.Text = Form1.x
        End Sub
    
        Private Sub ProBtn_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
            Dim filestr() As String
            Dim str As String
            If e.Data.GetDataPresent(DataFormats.FileDrop) Then
                filestr = e.Data.GetData(DataFormats.FileDrop)
                str = filestr(0)
                Me.FileName = str
                Me.SafeFileName = Me.FileName.Split("\").Last
                Me.Image = Icon.ExtractAssociatedIcon(Me.FileName).ToBitmap
                Form1.ListBox1.Items(Form1.x - 1) = Me.FileName
                Form1.ListBox2.Items(Form1.x - 1) = Me.SafeFileName
            Else
                Form1.SpeCon(Form1.x).FileName = Form1.s1b
                Form1.SpeCon(Form1.x).SafeFileName = Form1.s1c
                Form1.SpeCon(Form1.x).Image = Icon.ExtractAssociatedIcon(Form1.s1b).ToBitmap
    
                Me.FileName = Form1.s2b
                Me.SafeFileName = Form1.s2c
                Me.Image = Icon.ExtractAssociatedIcon(Form1.s2b).ToBitmap
            End If
        End Sub
    End Class
    I can drag and drop items from my desktop or wherever and apply certain properties and values to my button (ProBtn Class) but I don't know how to drag and drop between control to switch them. Can someone help me?

    Also, what are biscuits called in England? I forgot.
    Last edited by 957; October 1st, 2011 at 07:42 AM. Reason: more unnecessary bad jokes.

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Drag n' Drop between controls to switch values.

    Here is a full example of code to succesfully complete any kind of Drag and drop between any two objects in a project..

    Code:
        Private MouseIsDown As Boolean
    
        Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Item.AllowDrop = True 'Some objects dont allow this to be set in the Designer...
        End Sub
    
        Private Sub Item_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Item.MouseDown
            MouseIsDown = True
        End Sub
    
        Private Sub Item_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Item.MouseMove
                If MouseIsDown Then
                    'Start Drag and drop ..
                    'DDObject is whatever you need to pass via Drag and drop IE:
                    ': Text, integer, Object ...
                    Item.DoDragDrop(DDObject, DragDropEffects.Move)
                    Cursor.Current = Cursors.Hand
                End If
                MouseIsDown = False
            End If
        End Sub
    
        Private Sub Item_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Item.MouseUp
            MouseIsDown = False
        End Sub
    
        Private Sub Item_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Item.DragEnter
            If e.Data.GetDataPresent("Project.Form+ObjectType") Then
                'its one of our objecs.. it can drop here..
                e.Effect = DragDropEffects.Move
            End If
        End Sub
    
        Private Sub Item_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Item.DragDrop
            'its one of our objecs.. Lets drop it here..
            Item = e.Data.GetData(""Project.Form+ObjectType"")
        End Sub
    Hope it helps...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Mar 2011
    Posts
    153

    Re: Drag n' Drop between controls to switch values.

    Thanks GremilnSA. Your post inspired many ideas.

    I decided to instead move controls around in runtime with this
    Code:
    Private Sub ProBtn_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
            If Form1.Reorder.Checked = True Then
                dragging = True
                beginX = e.X
                beginY = e.Y
            End If
        End Sub
    
        Private Sub ProBtn_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
            If dragging = True Then
                Me.Location = New Point(Me.Location.X + e.X -
                beginX, Me.Location.Y + e.Y - beginY)
                Me.Refresh()
            End If
        End Sub
    
        Private Sub ProBtn_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
            dragging = False
        End Sub
    And drag and drop values from list view items added by clicking the add button with this
    Code:
    Private Sub ProBtn_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
            e.Effect = e.AllowedEffect
    
            Form1.Label2.Text = Me.Name
            Form1.s1a = Me.Name
            Form1.Label3.Text = Me.FileName
            Form1.s1b = Me.FileName
            Form1.Label4.Text = Me.SafeFileName
            Form1.s1c = Me.SafeFileName
            Form1.Label5.Text = Form1.x
            Form1.s1d = Form1.x
        End Sub
    
        Private Sub ProBtn_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragOver
            Form1.Label6.Text = Me.Name
            Form1.Label7.Text = Me.FileName
            Form1.Label8.Text = Me.SafeFileName
            Form1.Label9.Text = Form1.x
        End Sub
    
        Private Sub ProBtn_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
            Dim filestr() As String
            Dim str As String
            If e.Data.GetDataPresent(DataFormats.FileDrop) Then
                filestr = e.Data.GetData(DataFormats.FileDrop)
                str = filestr(0)
                Me.FileName = str
                Me.SafeFileName = Me.FileName.Split("\").Last
                Me.Image = Icon.ExtractAssociatedIcon(Me.FileName).ToBitmap
                Form1.ListBox1.Items(Form1.x - 1) = Me.FileName
                Form1.ListBox2.Items(Form1.x - 1) = Me.SafeFileName
            Else
    
                Me.FileName = Form1.LVuo.SelectedItems(0).SubItems(1).Text
                Me.SafeFileName = Form1.LVuo.SelectedItems(0).Text
                Me.Image = Form1.IL.Images.Item(Form1.SII)
            End If
        End Sub

  4. #4
    Join Date
    Mar 2011
    Posts
    153

    Re: Drag n' Drop between controls to switch values.

    I am still struggling with this. All I need to know is how to switch two control's locations.

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Drag n' Drop between controls to switch values.

    You would need to store one of the locations, say control1 then set control 1 location to control 2 location and control 2 to stored location where control 1 used to be.
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Mar 2011
    Posts
    153

    Re: Drag n' Drop between controls to switch values.

    Okay, I understand what you are saying but I do not know which two controls the user will select.
    How can I refer to a control with knowing its name in advance?

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Drag n' Drop between controls to switch values.

    When the user selects the controls then you should know which controls they have selected, until then you do not need to know.
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Mar 2011
    Posts
    153

    Re: Drag n' Drop between controls to switch values.

    I had a breakthrough and will show my results but first, I need to get something to work in order to do that.

    Buttons cannot have the itemdrag event which is needed to use this code:
    Code:
    DoDragDrop(e.Item, DragDropEffects.Move)
    How do I get around this?

  9. #9
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Drag n' Drop between controls to switch values.

    Thats why you use a combo of the MouseDown , MouseMove & MouseUp events (like shown in my previous post) to emulate the ItemDrag event...

    Code:
     Private Sub Item_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Item.MouseMove
                If MouseIsDown Then
                    'Start Drag and drop ..
                    'DDObject is whatever you need to pass via Drag and drop IE:
                    ': Text, integer, Object ...
                    Item.DoDragDrop(DDObject, DragDropEffects.Move)
                    Cursor.Current = Cursors.Hand
                End If
                MouseIsDown = False
        End Sub
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured