Results 1 to 9 of 9

Thread: [RESOLVED] Picturebox help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    83

    Resolved [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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Picturebox help

    you could store the image name in the picturebox tag property, then change that as you DnD

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    83

    Re: Picturebox help

    Quote Originally Posted by .paul. View Post
    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

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Picturebox help

    if you post the code you're using i'll show you how to integrate what i suggested

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    83

    Re: Picturebox help

    Quote Originally Posted by .paul. View Post
    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

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Picturebox help

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Item1.Image = My.Resources.[resourceName1]
    5.         Item1.Tag = "[resourceName1]"
    6.         MainItem.Image = My.Resources.[resourceName2]
    7.         MainItem.Tag = "[resourceName2]"
    8.         Label1.Text = "Item1.Tag = " & Item1.Tag
    9.         Label2.Text = "MainItem.Tag = " & MainItem.Tag
    10.         Item1.AllowDrop = True
    11.         MainItem.AllowDrop = True
    12.     End Sub
    13.  
    14.     Dim description As String
    15.  
    16.     Private Sub Item1_Mousedown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Item1.MouseDown
    17.         'Source PictureBox
    18.         If e.Button = MouseButtons.Left Then
    19.             description = Item1.Tag.ToString
    20.             Item1.DoDragDrop(Item1.Image, DragDropEffects.Move)
    21.         End If
    22.     End Sub
    23.  
    24.     Private Sub MainItem_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragDrop
    25.         'Set the image to be the dragged image.
    26.         If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
    27.             TempImage.Image = MainItem.Image
    28.             TempImage.Tag = MainItem.Tag
    29.             MainItem.Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)
    30.             MainItem.Tag = description
    31.             Item1.Image = TempImage.Image
    32.             Item1.Tag = TempImage.Tag
    33.             Label1.Text = "Item1.Tag = " & Item1.Tag
    34.             Label2.Text = "MainItem.Tag = " & MainItem.Tag
    35.         End If
    36.     End Sub
    37.  
    38.     Private Sub MainItem_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragOver
    39.         'Target PictureBox
    40.         'Drag Drop Effects
    41.         If e.Data.GetDataPresent(DataFormats.Bitmap) Then
    42.             e.Effect = DragDropEffects.Move
    43.         Else
    44.             e.Effect = DragDropEffects.None
    45.         End If
    46.     End Sub
    47.  
    48. End Class

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    83

    Re: Picturebox help

    Quote Originally Posted by .paul. View Post
    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Item1.Image = My.Resources.[resourceName1]
    5.         Item1.Tag = "[resourceName1]"
    6.         MainItem.Image = My.Resources.[resourceName2]
    7.         MainItem.Tag = "[resourceName2]"
    8.         Label1.Text = "Item1.Tag = " & Item1.Tag
    9.         Label2.Text = "MainItem.Tag = " & MainItem.Tag
    10.         Item1.AllowDrop = True
    11.         MainItem.AllowDrop = True
    12.     End Sub
    13.  
    14.     Dim description As String
    15.  
    16.     Private Sub Item1_Mousedown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Item1.MouseDown
    17.         'Source PictureBox
    18.         If e.Button = MouseButtons.Left Then
    19.             description = Item1.Tag.ToString
    20.             Item1.DoDragDrop(Item1.Image, DragDropEffects.Move)
    21.         End If
    22.     End Sub
    23.  
    24.     Private Sub MainItem_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragDrop
    25.         'Set the image to be the dragged image.
    26.         If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
    27.             TempImage.Image = MainItem.Image
    28.             TempImage.Tag = MainItem.Tag
    29.             MainItem.Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)
    30.             MainItem.Tag = description
    31.             Item1.Image = TempImage.Image
    32.             Item1.Tag = TempImage.Tag
    33.             Label1.Text = "Item1.Tag = " & Item1.Tag
    34.             Label2.Text = "MainItem.Tag = " & MainItem.Tag
    35.         End If
    36.     End Sub
    37.  
    38.     Private Sub MainItem_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragOver
    39.         'Target PictureBox
    40.         'Drag Drop Effects
    41.         If e.Data.GetDataPresent(DataFormats.Bitmap) Then
    42.             e.Effect = DragDropEffects.Move
    43.         Else
    44.             e.Effect = DragDropEffects.None
    45.         End If
    46.     End Sub
    47.  
    48. 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.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    83

    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.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [RESOLVED] Picturebox help

    here's a solution:

    vb Code:
    1. Public Class Form1
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         Item1.Image = My.Resources.[resourceName1]
    4.         Item1.Tag = "[resourceName1]"
    5.         Item2.Image = My.Resources.[resourceName2]
    6.         Item2.Tag = "[resourceName2]"
    7.         MainItem.Image = My.Resources.[resourceName3]
    8.         MainItem.Tag = "[resourceName3]"
    9.         Label1.Text = "Item1.Tag = " & Item1.Tag
    10.         Label2.Text = "Item2.Tag = " & Item2.Tag
    11.         Label3.Text = "MainItem.Tag = " & MainItem.Tag
    12.         Item1.AllowDrop = True
    13.         Item2.AllowDrop = True
    14.         MainItem.AllowDrop = True
    15.     End Sub
    16.  
    17.     Dim source As PictureBox
    18.  
    19.     Private Sub Items_Mousedown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Item1.MouseDown, Item2.MouseDown
    20.         source = DirectCast(sender, PictureBox)
    21.         'Source PictureBox
    22.         If e.Button = MouseButtons.Left Then
    23.             source.DoDragDrop(source.Image, DragDropEffects.Move)
    24.         End If
    25.     End Sub
    26.  
    27.     Private Sub MainItem_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragDrop
    28.         'Set the image to be the dragged image.
    29.         If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
    30.             TempImage.Image = MainItem.Image
    31.             TempImage.Tag = MainItem.Tag
    32.             MainItem.Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)
    33.             MainItem.Tag = source.Tag
    34.             source.Image = TempImage.Image
    35.             source.Tag = TempImage.Tag
    36.             Label1.Text = "Item1.Tag = " & Item1.Tag
    37.             Label2.Text = "Item2.Tag = " & Item2.Tag
    38.             Label3.Text = "MainItem.Tag = " & MainItem.Tag
    39.         End If
    40.     End Sub
    41.     Private Sub MainItem_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MainItem.DragOver
    42.         'Target PictureBox
    43.         'Drag Drop Effects
    44.         If e.Data.GetDataPresent(DataFormats.Bitmap) Then
    45.             e.Effect = DragDropEffects.Move
    46.         Else
    47.             e.Effect = DragDropEffects.None
    48.         End If
    49.     End Sub
    50. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width