Hey I've created a dictionary that I use to create pictureboxes. Each picturebox has a number, which I then want to use to remove the access record, corresponding to that number. So I've got the dictionary working, but how do I get the number that corresponds to each picturebox? Here is my code (everything is defined, like cardsdictionary as dictionary):

   Public Sub bigpictureloader()
       'Dim list As New List(Of String)(cardsdictionary.Keys)

       Dim cardcount As Integer
       cardcount = 0
       counter += 1
       cardcount = counter
       'Dim cards As List(Of String) = New List(Of String)
       cardsdictionary.Add(imageurltxt.Text, cardcount)


       'Create a placeholder variable
       Dim cardPictureBox As PictureBox
       Dim pair As KeyValuePair(Of String, Integer)
       'Loop through every selected card URL
       For Each pair In cardsdictionary
           'Create a new PictureBox
           cardPictureBox = New PictureBox()
           cardPictureBox.Size = New Size(100, 100)
           cardPictureBox.SizeMode = PictureBoxSizeMode.Zoom
           cardPictureBox.WaitOnLoad = False
           AddHandler cardPictureBox.Click, AddressOf imagehandler

           'Add the PictureBox to the Form
           Me.Controls.Add(cardPictureBox)
           'MsgBox(cardsdict.Values.ToString)

           If imageurltxt.Text = "" Then
               cardPictureBox = Nothing
           Else

               cardPictureBox.LoadAsync(pair.Key)
               TableLayoutPanel1.Controls.Add(cardPictureBox, 0, 0)

           End If

       Next

   End Sub

    Private Sub testdelete()
        'THIS SAVES TO THE DEBUG ACCESS DATABASE!!!!!
        Using conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = FULL YUGIOH ACCESS DATABASE.accdb;")
            Using command As New OleDbCommand("Delete From cmon11 Where ID= @ID;", conn)
'pair.value is what I think will work, but doesn't currently               
command.Parameters.Add("@ID", OleDbType.Integer).Value = CInt(pair.value)
                conn.Open()
                command.ExecuteNonQuery()
            End Using
        End Using


    End Sub

this is my imagehandler!!!

Private Sub imagehandler(Sender As Object, e As EventArgs)
       testdelete()
    End Sub

2 Answers

0
Michael Z. On Best Solutions

You can use the Tag property of the PictureBox to store the ID. You populate this property when you create the PictureBox.

In your handler you can do something like this:

Private Sub imagehandler(Sender As Object, e As EventArgs)
           Dim myPictureBox As PictureBox = CType(sender, PictureBox)
           'Now you can access myPictureBox.Tag and pass it into your delete function
           Dim id As Integer = -1
           If Int32.TryParse(myPictureBox.Tag, id) = True Then
                testdelete(id)
           End If           
        End Sub


Private Sub testdelete(id As Integer)
        'THIS SAVES TO THE DEBUG ACCESS DATABASE!!!!!
        Using conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = FULL YUGIOH ACCESS DATABASE.accdb;")
            Using command As New OleDbCommand("Delete From cmon11 Where ID= @ID;", conn)
'pair.value is what I think will work, but doesn't currently               
command.Parameters.Add("@ID", OleDbType.Integer).Value = id
                conn.Open()
                command.ExecuteNonQuery()
            End Using
        End Using


    End Sub
0
SezMe On

testdelete doesn't know which imasge you want to delete. Using Michael's suggestion you want:

command.Parameters.Add("@ID", OleDbType.Integer).Value = CInt(Me.Tag)

By the way your use of counter and cardcount can be cleaned up substantially.