Click here to Skip to main content
15,892,927 members

How do I remove a panel in VB.NET

Willtwinny asked:

Open original thread
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
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900