[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

Drag ' n ' Drop from Listview to Panel

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.
0
Milkus1
Asked:
Milkus1
  • 2
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.

~IM
0
 
Milkus1Author Commented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'Panel1
        '
        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
        '
        'Button1
        '
        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"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(464, 390)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Panel1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    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
            Try
                Dim pb As New PictureBox
                pb.Image = Image.FromFile(OpenFileDialog1.FileName)
                pb.Location = New Point(0, 0)
                pb.Size = New Size(100, 100)
                Panel1.Controls.Add(pb)
                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
0
 
Milkus1Author Commented:
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now