Groups | Blog | Home
all groups > vb.net controls > august 2004 >

vb.net controls : Remove Added Buttons?


John Rugo
8/11/2004 4:59:05 PM
Hi All,
I am getting errors when trying to remove buttons from a form that I added
via code.

I have a Panel called panBTN
I have added buttons to it... Several buttons

Private Sub AddButton()
btnScrMgr = New Button
With btnScrMgr
.Parent = panBTN
.Text = dvr.Item("SCRName").ToString
.Tag = MgrKey$ & "][" & MgrID$
.FlatStyle = FlatStyle.Flat
.Dock = DockStyle.Top
.BringToFront()
End With
AddHandler btnScrMgr.Paint, AddressOf PaintXPButton
etc.....
End Sub
(The Above Works like a charm :))

I reset the buttons by first removing all the ones I created. The code thus
far has ended up like this:

Dim btn As Button
For Each btn In panBTN.Controls
Try
panBTN.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Next

I also tried the following getting NO errors but NOT removing anything
either:

Dim btn As Control
For Each btn In panBTN.Controls
Try
panBTN.Controls.Remove(btn)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Next

Any ideas would be great!
Thanks,
John.


Andy Becker
8/12/2004 5:20:32 PM
[quoted text, click to view]

Here is a deceptively simple way I stumbled upon:

While panBTN.Controls.Count > 0
panBTN.Controls.RemoveAt(0)
End While

Best Regards,

Andy

John Rugo
8/12/2004 10:24:54 PM
I can't believe it was that simple. Thank you for helpping me :)

[quoted text, click to view]

Andy Becker
8/16/2004 9:35:35 AM
[quoted text, click to view]

It is actually even easier... I was thinking in a mode where I was moving
controls from one parent to another, and needed a loop. You can use the one
line technique which Herfried pointed out in another thread:

panBtn.Controls.Clear()

Best Regards,

Andy

AddThis Social Bookmark Button