Okay this may seem like an easy question but I can't seem to actually get it to work, and wonder if anybody can help me, or point me in the right direction as I have searched for an answer already but have drawn up blanks.
What I have is a flow layout panel, as the main panel on my Form. The reason for a flow layout panel is so I can easily add other panels to it without specifying location points (actually putting my Java layout manager knowledge to good use).
These other panels that are added to the flow layout panel are added dynamically by the actual user with a button marked "Add Panel". The button name is called btnAddPanel. When these panels are added, a button is also included in this panel (btndeleteButton) which when the user clicks is suppose to delete the panel which the button resides on (the parent panel of the button).
The problem is I can't seem to get the delete button to work. I sometimes can get the last panel that has been added to be removed, but even this is sometimes hit and miss.
When the user clicks the delete button on one of the panels, the parent panel needs to be removed.
This is the code I have already and the commented out parts are some left over pieces of code I have tried but failed to work. I have tried a lot more times to get it to work, but every attempt I have failed.
Any suggestions? Thanks :)
Public Class Form1
Dim btndeleteButton As Button
Dim dynamicPanel As Panel
Private Sub btnAddPanel_Click(sender As System.Object, e As System.EventArgs) Handles btnAddPanel.Click
dynamicPanel = New Panel
dynamicPanel.Size = New System.Drawing.Size(357, 100)
dynamicPanel.BackColor = Color.LightBlue
btndeleteButton = New Button
btndeleteButton.Width = 100
btndeleteButton.Text = "Delete"
AddHandler btndeleteButton.Click, AddressOf btndeleteButton_Click
dynamicPanel.Controls.Add(btndeleteButton)
FlowLayoutPanel1.Controls.Add(dynamicPanel)
End Sub
Private Sub btndeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'FlowLayoutPanel1().Controls.Remove(dynamicPanel)
'btndeleteButton.Parent.Dispose()
End Sub
End Class