Click here to Skip to main content
16,022,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the following code ,i can drag an item in form.but it is same for all node.i want to specify an node & according to that picturebox will generate.i used "item.text" property in itemdrag event but it's note working.


VB
Public Class Form1
    Dim pic As New PictureBox
    Public drag As Boolean = False
    Public mousex, mousey As Integer

    Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
        Dim pic As New PictureBox
        Dim rand As New Random



        Randomize(System.DateTime.Now.Millisecond)
        Threading.Thread.Sleep(10)
        pic.Location = New Point((500 * Rnd()), (500 * Rnd()))
        pic.BackColor = Color.FromArgb(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0, 256))
        Me.Controls.Add(pic)
        AddHandler pic.MouseDown, AddressOf pic_MouseDown
        AddHandler pic.MouseMove, AddressOf pic_Mousemove
        AddHandler pic.MouseUp, AddressOf pic_Mouseup
        
    End Sub
    Private Sub Form1_DragEnter1(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.AllowDrop = True
        TreeView1.AllowDrop = False
    End Sub

    Private Sub TreeView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragOver
        e.Effect = DragDropEffects.Link
    End Sub

    Private Sub TreeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag
        DoDragDrop(e.Item, DragDropEffects.Copy)
    End Sub

    Private Sub pic_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Me.Cursor = Cursors.Hand
            drag = True
            mousex = -e.X
            mousey = -e.Y
            Dim left As Integer = Me.PointToClient(MousePosition).X - sender.location.x
            Dim right As Integer = Me.PointToClient(MousePosition).Y - sender.location.y
            Dim width As Integer = Me.ClientSize.Width - (sender.width - left)
            Dim height As Integer = Me.ClientSize.Height - (sender.width - right)
            Windows.Forms.Cursor.Clip = Me.RectangleToScreen(New Rectangle(left, right, width, height))
            sender.invalidate()
        End If
    End Sub

    Private Sub pic_Mousemove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If drag Then
            Dim mposition As New Point()
            mposition = Me.PointToClient(MousePosition)
            mposition.Offset(mousex, mousey)
            sender.location = mposition
        End If
    End Sub

    Private Sub pic_Mouseup(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        drag = False
        Windows.Forms.Cursor.Clip = Nothing
        Me.Cursor = Cursors.Arrow
        sender.invalidate()
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.Show()
        Me.Enabled = False
    End Sub

End Class


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 15-Jun-12 20:28pm
v2
Comments
OriginalGriff 16-Jun-12 2:32am    
Just so you know for future questions: the "var" button inserts "code" tags, which is for inline code (variable names in your text description of a problem, that sort of thing). The "code" button inserts "pre" tags which lets you select the language, preserve formatting, and engage the syntax colourizer - this is much, much better for code fragments! I have modified your question, have a look at see how much easier it is for us to read!

1 solution

Why do you declare 2 times?
Dim pic as new picture box is writen 2 times.
You have created that globaly you dont need to create it twice.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900