Drag ' n ' Drop from Listview to Panel

Posted on 2004-11-22
Last Modified: 2010-04-23
I am currently building an application that allows the user to set up the dimensions of a room, then add pieces of funiture to the room (all to scale).
I was wanting to use the listview control for showing the furniture items, and a panel control is used as a 'snap grid' room.
Now the icons in the listview are not the same ones to be dragged onto the panel. What I would like to be able to do is either drag n drop the correctly scaled piece of furniture onto the panel, or have it appear on the panel when selected( much like a Real time strategy game). The user can then position it, rotate etc as needed.

Background out of the way, how do I select the right size graphic when I click on the listview, display it for dragging and drop it onto the panel. I have only 3 ratios to work with (1:1, 7:10, 1:2), which is determined when the room dimensions are chosen. May initial thoughts is to just build graphics in all 3 dimensions and stick them in a imagelist, on selection choose the correct sized graphic and then create a picturebox on the panel to stick it in. Sounds okay in theory, but VB.NET is not VB6, which is where I'm falling down. Any help is appreciated.
Question by:Milkus1
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Not sure exactly where you want help at...

    Seperate PictureBoxes for each piece of furniture will make moving them easy.  Also, if you set the AutoScroll property of the Panel to True then you have an instant scrollable room.

    You can use the DrawImage() function to scale your graphics for the different ratios.


    Author Comment

    Thanks IdleMind,
    what I need help with is how do I make the picturebox array at runtime, in VB.NET and the handlers for the boxes. Or display the correct graphic for dragging onto the panel? A room may have 50 pieces of furniture, all which need to be tracked, rotated, snapped to a grid. etc.
    If I could do it like SIMCITY or Warcraft type mechanics I would be very happy. You know where the 'piece' is moved around with the cursor until its dropped.
    Currently I can click an Item and make 1 picturebox appear in the room with the correct graphic, but not an array of them.

     I'll check into the drawimage function to handle the sizing issue.
    LVL 85

    Accepted Solution

    Here is a very simple example of how to add multiple, draggable pictureboxes to a panel:

    Public Class Form1
        Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

        Public Sub New()

            'This call is required by the Windows Form Designer.

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                End If
            End If
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents Panel1 As System.Windows.Forms.Panel
        Friend WithEvents Button1 As System.Windows.Forms.Button
        Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.Panel1 = New System.Windows.Forms.Panel
            Me.Button1 = New System.Windows.Forms.Button
            Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
            Me.Panel1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.Panel1.AutoScroll = True
            Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
            Me.Panel1.Location = New System.Drawing.Point(80, 8)
            Me.Panel1.Name = "Panel1"
            Me.Panel1.Size = New System.Drawing.Size(376, 376)
            Me.Panel1.TabIndex = 0
            Me.Button1.Location = New System.Drawing.Point(8, 8)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(64, 40)
            Me.Button1.TabIndex = 1
            Me.Button1.Text = "Add Image"
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(464, 390)
            Me.Name = "Form1"
            Me.Text = "Form1"

        End Sub

    #End Region

        Private startPoint As Point

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            OpenFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"
            If OpenFileDialog1.ShowDialog = DialogResult.OK Then
                    Dim pb As New PictureBox
                    pb.Image = Image.FromFile(OpenFileDialog1.FileName)
                    pb.Location = New Point(0, 0)
                    pb.Size = New Size(100, 100)
                    AddHandler pb.MouseDown, AddressOf pb_MouseDown
                    AddHandler pb.MouseMove, AddressOf pb_MouseMove
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error Loadinng Image")
                End Try
            End If
        End Sub

        Private Sub pb_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            If e.Button = MouseButtons.Left Then
                Dim pb As PictureBox = CType(sender, PictureBox)
                startpoint = pb.PointToScreen(New Point(e.X, e.Y))
            End If
        End Sub

        Private Sub pb_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            If e.Button = MouseButtons.Left Then
                Dim pb As PictureBox = CType(sender, PictureBox)
                Dim curPoint As Point = pb.PointToScreen(New Point(e.X, e.Y))
                pb.Left = pb.Left + (curPoint.X - startPoint.X)
                pb.Top = pb.Top + (curPoint.Y - startPoint.Y)
                startPoint = curPoint
            End If
        End Sub

    End Class

    Author Comment

    Legend!!! Thanks Idle Mind...all points for you this looks exactly what I need!
    I love this site, and guys like you who help out.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    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…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now