So I have 2 picture boxes, a textbox, and a button. One of the picture boxes adds another row with the same controls but moved down when clicked and the other picture box (should) remove the same row that it was clicked on. I have PictureBoxAdd1, PictureBoxRemove1, TextBox1, and Button1 set up by default and when PictureBoxAdd1 is clicked, it adds 1 at the end of the name properties. Now, how do I remove the appropriate row when PictureBoxRemove is clicked? Say I have 5 rows (but the amount of rows available will depend on how many PictureBoxAdd was clicked), if I click on PictureBoxRemove4, then PictureBoxAdd4, PictureBoxRemove4, TextBox4, and Button4 should be removed all together. Any help would be greatly appreciated.

Recommended Answers

All 3 Replies

See if this helps.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '// when adding new row of controls, add a Handler for your PictureBoxRemove.
        AddHandler PictureBoxRemove1.Click, AddressOf PictureBoxRemove_Click
        'AddHandler PictureBoxRemove2.Click, AddressOf PictureBoxRemove_Click
        'AddHandler PictureBoxRemove3.Click, AddressOf PictureBoxRemove_Click
    End Sub

    Private Sub PictureBoxRemove_Click(sender As System.Object, e As System.EventArgs)
        '// get name of sender and remove the name except for the end #.
        Dim sNumber As String = CType(sender, PictureBox).Name.Replace("PictureBoxRemove", "")
        With Panel1 '// your Panel.
            '// since you now have the # for the row, delete all controls that end w/that #.
            .Controls.Remove(CType(.Controls("PictureBoxAdd" & sNumber), PictureBox))
            .Controls.Remove(CType(.Controls("PictureBoxRemove" & sNumber), PictureBox))
            .Controls.Remove(CType(.Controls("TextBox" & sNumber), TextBox))
            .Controls.Remove(CType(.Controls("Button" & sNumber), Button))
        End With
    End Sub
End Class

Wow, that is so much easier than what I was trying to do from Google search results. Thanks a lot.

You should try a "codeorder" search next time, might get better results. :D

Glad I could help. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.