|
-
Jun 16th, 2010, 03:35 PM
#1
Thread Starter
Lively Member
[RESOLVED] Picturebox help
Hi, i've placed the images I want to use in the projects resource file (any old image to test), and i've loaded them into the picture boxes when the application loads.
I use drag & drop and can change the picture from one box to the other, and basicaly swap them around. One problem.... I can't figure out what picture is in which box after i've moved them.
Is there any way to somhow receive or return the name of the picture currently in a picturebox (i.e the name of the image in the resource file)
Thanks.
-
Jun 16th, 2010, 03:40 PM
#2
Re: Picturebox help
you could store the image name in the picturebox tag property, then change that as you DnD
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 16th, 2010, 03:45 PM
#3
Thread Starter
Lively Member
Re: Picturebox help
 Originally Posted by .paul.
you could store the image name in the picturebox tag property, then change that as you DnD
Hmm never heard of that property or used picture boxes much, can you elaborate?
Thanks
-
Jun 16th, 2010, 03:49 PM
#4
Re: Picturebox help
if you post the code you're using i'll show you how to integrate what i suggested
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 16th, 2010, 03:57 PM
#5
Thread Starter
Lively Member
Re: Picturebox help
 Originally Posted by .paul.
if you post the code you're using i'll show you how to integrate what i suggested
Dragging and dropping to another picturebox then putting the image that got replaced into other (i.e swapping them around) :
Code:
Private Sub Item1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Item1.MouseMove
'Source PictureBox
If e.Button = MouseButtons.Left Then
Item1.DoDragDrop(Item1.Image, DragDropEffects.All)
End If
End Sub
Private Sub MainItem_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragDrop
'Set the image to be the dragged image.
If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
TempImage.Image = MainItem.Image
Me.MainItem.Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)
Item1.Image = TempImage.Image
End If
End Sub
Private Sub MainItem_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragEnter
'Target PictureBox
'Drag Drop Effects
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
-
Jun 16th, 2010, 04:35 PM
#6
Re: Picturebox help
try this:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Item1.Image = My.Resources.[resourceName1]
Item1.Tag = "[resourceName1]"
MainItem.Image = My.Resources.[resourceName2]
MainItem.Tag = "[resourceName2]"
Label1.Text = "Item1.Tag = " & Item1.Tag
Label2.Text = "MainItem.Tag = " & MainItem.Tag
Item1.AllowDrop = True
MainItem.AllowDrop = True
End Sub
Dim description As String
Private Sub Item1_Mousedown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Item1.MouseDown
'Source PictureBox
If e.Button = MouseButtons.Left Then
description = Item1.Tag.ToString
Item1.DoDragDrop(Item1.Image, DragDropEffects.Move)
End If
End Sub
Private Sub MainItem_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragDrop
'Set the image to be the dragged image.
If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
TempImage.Image = MainItem.Image
TempImage.Tag = MainItem.Tag
MainItem.Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)
MainItem.Tag = description
Item1.Image = TempImage.Image
Item1.Tag = TempImage.Tag
Label1.Text = "Item1.Tag = " & Item1.Tag
Label2.Text = "MainItem.Tag = " & MainItem.Tag
End If
End Sub
Private Sub MainItem_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragOver
'Target PictureBox
'Drag Drop Effects
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 16th, 2010, 05:15 PM
#7
Thread Starter
Lively Member
Re: Picturebox help
 Originally Posted by .paul.
try this:
vb Code:
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Item1.Image = My.Resources.[resourceName1] Item1.Tag = "[resourceName1]" MainItem.Image = My.Resources.[resourceName2] MainItem.Tag = "[resourceName2]" Label1.Text = "Item1.Tag = " & Item1.Tag Label2.Text = "MainItem.Tag = " & MainItem.Tag Item1.AllowDrop = True MainItem.AllowDrop = True End Sub Dim description As String Private Sub Item1_Mousedown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Item1.MouseDown 'Source PictureBox If e.Button = MouseButtons.Left Then description = Item1.Tag.ToString Item1.DoDragDrop(Item1.Image, DragDropEffects.Move) End If End Sub Private Sub MainItem_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragDrop 'Set the image to be the dragged image. If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then TempImage.Image = MainItem.Image TempImage.Tag = MainItem.Tag MainItem.Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap) MainItem.Tag = description Item1.Image = TempImage.Image Item1.Tag = TempImage.Tag Label1.Text = "Item1.Tag = " & Item1.Tag Label2.Text = "MainItem.Tag = " & MainItem.Tag End If End Sub Private Sub MainItem_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragOver 'Target PictureBox 'Drag Drop Effects If e.Data.GetDataPresent(DataFormats.Bitmap) Then e.Effect = DragDropEffects.Move Else e.Effect = DragDropEffects.None End If End Sub End Class
Nice that works, one more thing:
I added another picturebox (Item2) which can swap with the main picture box(mainitem) the same as item1. However, when I drag that image to the main picturebox, it doesn't know which picturebox the image came from so it doesn't know if to put the image from the main into image1 or image2.
I tryed adding a IF statement and seeing if the tag contains the text item1, if so replace item1's picturebox, however it didn't work and went a bit odd (I didn't save it incase that happened). Any way to see the source picturebox where the item was received from?
Thanks,
I repped you.
-
Jun 16th, 2010, 06:04 PM
#8
Thread Starter
Lively Member
Re: Picturebox help
fixed, don't need help now unless there's a built in method that tells u where the drag & drop came from, just used a variable.
-
Jun 16th, 2010, 06:09 PM
#9
Re: [RESOLVED] Picturebox help
here's a solution:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Item1.Image = My.Resources.[resourceName1]
Item1.Tag = "[resourceName1]"
Item2.Image = My.Resources.[resourceName2]
Item2.Tag = "[resourceName2]"
MainItem.Image = My.Resources.[resourceName3]
MainItem.Tag = "[resourceName3]"
Label1.Text = "Item1.Tag = " & Item1.Tag
Label2.Text = "Item2.Tag = " & Item2.Tag
Label3.Text = "MainItem.Tag = " & MainItem.Tag
Item1.AllowDrop = True
Item2.AllowDrop = True
MainItem.AllowDrop = True
End Sub
Dim source As PictureBox
Private Sub Items_Mousedown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Item1.MouseDown, Item2.MouseDown
source = DirectCast(sender, PictureBox)
'Source PictureBox
If e.Button = MouseButtons.Left Then
source.DoDragDrop(source.Image, DragDropEffects.Move)
End If
End Sub
Private Sub MainItem_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragDrop
'Set the image to be the dragged image.
If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
TempImage.Image = MainItem.Image
TempImage.Tag = MainItem.Tag
MainItem.Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)
MainItem.Tag = source.Tag
source.Image = TempImage.Image
source.Tag = TempImage.Tag
Label1.Text = "Item1.Tag = " & Item1.Tag
Label2.Text = "Item2.Tag = " & Item2.Tag
Label3.Text = "MainItem.Tag = " & MainItem.Tag
End If
End Sub
Private Sub MainItem_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragOver
'Target PictureBox
'Drag Drop Effects
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|