954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Remove Appropriate Row Controls In Panel

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.

secretply
Newbie Poster
6 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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

secretply
Newbie Poster
6 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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

Glad I could help. :)

codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: