?
Solved

VB.NET 2005 Drag and Drop item from List onto PictureBox

Posted on 2006-04-10
5
Medium Priority
?
449 Views
Last Modified: 2012-05-05
Hello, Experts!
Let say I have few PictureBoxes, and one ListBox, I want to be able drag and drop one element from ListBox onto PictureBox, on Drop event I want to see the MsgBox
With information what Item I just dropped and on what control I did it

Any help greatly appreciated.
0
Comment
Question by:bogdem
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:jjardine
ID: 16423833
The first thing I would do is set up a method to handle the drop event on the Picture Boxes Like this:

Public Sub PictureBox_DragDrop(sender as Object, e as System.Windows.Forms.DragEventArgs)

End Sub

After creating the method I would add handlers for all of the picture boxes to be handled by this method.  Maybe do this in the Form_Load event.Ex.

AddHandler PictureBox1.DragDrop, AddressOf PictureBox_DragDrop
AddHandler PictureBox2.DragDrop, AddressOf PictureBox_DragDrop

Don't forget to set each picture box's AllowDrop property to True

On MouseDown of the Listbox you want to capture the information you want to pass to your picturebox.  This could be a reference to the listbox object or some other custom object you create to capture the information you want to send.   an example of passing the listbox would be something like this  coded in the ListBox_mouseDown Event.

listbox1.doDragDrop(listbox1,DragDropEffects.All)

Then in the PictureBox_DragDrop method first listed in this example  you could add the following code:

MessageBox.Show(DirectCast(Sender,PictureBox).Name & " is sending: " & DirectCast(e.Data,ListBox).SelectedItem)

I hope this helps get you started on working with this.  This is just a generic example of how to complete the drag drop and get general information.  You can get many other fields from the objects passed.
jjardine
0
 

Author Comment

by:bogdem
ID: 16425808
Sorry, couldn’t make this code work.
0
 
LVL 14

Expert Comment

by:jjardine
ID: 16426548
What version of .net are you using??   2003 or 2005?
0
 
LVL 14

Accepted Solution

by:
jjardine earned 2000 total points
ID: 16426717
I just did this sample code in 2005   and here are the results..  I added 2 picture boxes to the form and a listbox.  the listbox contains 2 items (Hello, Goodbye).   Then here is the code for when I drag an element from the listbox to the picturebox.  

  Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    PictureBox1.AllowDrop = True
    PictureBox2.AllowDrop = True

    AddHandler PictureBox1.DragDrop, AddressOf PictureBox_DragDrop
    AddHandler PictureBox2.DragDrop, AddressOf PictureBox_DragDrop

    AddHandler PictureBox1.DragEnter, AddressOf PictureBox_DragEnter
    AddHandler PictureBox2.DragEnter, AddressOf PictureBox_DragEnter
  End Sub

  Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
    ListBox1.DoDragDrop(ListBox1, DragDropEffects.Copy)
  End Sub

  Private Sub PictureBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
    MessageBox.Show(DirectCast(sender, PictureBox).Name & " received this data: " & DirectCast(e.Data.GetData(GetType(ListBox)), ListBox).SelectedItem)

  End Sub


  Private Sub PictureBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
    e.Effect = DragDropEffects.Copy
  End Sub
0
 

Author Comment

by:bogdem
ID: 16430742
Thanks a lot.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

621 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