This is part of the last coding that codeorder helped me with. I didn't include this in the last post because I thought this was something that I was going to be able to do. Unfortunately, it is not working properly. In addition to the controls being removed, the control rows after the removed row should be changed so it's still in sequential order.

Private Sub PictureBoxRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  Dim RowRemove As String = CType(sender, PictureBox).Name.Replace("PictureBoxRemove", "")

  PanelArea.Controls.Remove(CType(PanelArea.Controls("PictureBoxAdd" + RowRemove.ToString), PictureBox))
  PanelArea.Controls.Remove(CType(PanelArea.Controls("PictureBoxRemove" + RowRemove.ToString), PictureBox))
  PanelArea.Controls.Remove(CType(PanelArea.Controls("TextBox" + RowRemove.ToString), TextBox))
  PanelArea.Controls.Remove(CType(PanelArea.Controls("Button" + RowRemove.ToString), Button))

  Dim RowBefore As Integer = CInt(SegmentRemove) + 1
  Dim RowAfter As Integer = CInt(SegmentRemove)

  While RowBefore <= RowTotal
    ' Replace all controls names below to replace the removed control.
    PanelArea.Controls("PictureBoxAdd" + RowBefore.ToString).Name.Replace("PictureBoxAdd" + RowBefore.ToString, "PictureBoxAdd" + RowAfter.ToString)
    PanelArea.Controls("PictureBoxRemove" + RowBefore.ToString).Name.Replace("PictureBoxRemove" + RowBefore.ToString, "PictureBoxRemove" + RowAfter.ToString)
    PanelArea.Controls("TextBox" + RowBefore.ToString).Name.Replace("TextBox" + RowBefore.ToString, "TextBox" + RowAfter.ToString)
    PanelArea.Controls("Button" + RowBefore.ToString).Name.Replace("Button" + RowBefore.ToString, "Button" + RowAfter.ToString)

    ' Move up all controls below removed control to takes its place.
    PanelArea.Controls("PictureBoxAdd" + SegmentAfter.ToString).Top -= 30
    PanelArea.Controls("PictureBoxRemove" + SegmentAfter.ToString).Top -= 30
    PanelArea.Controls("TextBox" + SegmentAfter.ToString).Top -= 30
    PanelArea.Controls("Button" + SegmentAfter.ToString).Top -= 30

    RowBefore += 1
    RowAfter += 1
  End While

  RowTotal -= 1
End Sub

Before this code, "PanelArea.Controls("PictureBoxAdd" + RowBefore.ToString).Name.Replace("PictureBoxAdd" + RowBefore.ToString, "PictureBoxAdd" + RowAfter.ToString)" I used the same coding in a MsgBox() so I can determine what the new PictureBoxAdd name was going to be and it showed the correct value but I'm getting some error that's related to the object is not being referenced. I hope this is clear enough to understand what I wanted to do. Thanks.

Recommended Answers

All 2 Replies

Which line is giving you the error?

>>...the control rows after the removed row should be changed so it's still in sequential order.
Sequential order for the # at the end of each control's name or just moving the rows under the delete row up?

The error occurs with moving the controls up, saying that "object reference not set to an instance of an object" but I believe the error is caused by renaming the controls, as it looks like it is not giving the new controls the correct name so the looping isn't finding the correct control name.

Edit: I was able to fix the error, instead of replacing it, I just grabbed the name of the control and set it equal to a new name. Thanks for the response.

Be a part of the DaniWeb community

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