KAllbritain

What's the best method to delete, dispose, destroy a dynamically
rendered screen object, (i.e. textbox, combobox), programmatically

My app lets the user paint different objects to the screen and therefore must allow them to change their mind and remove them.


I keep all created controls in a datatable, then when the clear button is selected, I read the controls on the page, compare them to the datatable entries and then try to delete them with no success.

I have tried:

Private Sub clearScreen(ByVal container As Control)
Dim ctl As Control
Dim tmpRow As DataRow

For Each ctl In container.Controls
'skip static controls
For Each tmpRow In workTable.Rows
If tmpRow("name").Equals(ctl.Name) Then
ctl.Visible = False
ctl.Dispose()
End If
Next
Next
End Sub




Thoughts




Re: Visual Basic General Deleting screen objects programmatically

spotty

When you delete the controls - you simply want to remove them from the controls collection of the form

Here's a simple example which adds 5 buttons on form load - click button to remove 2 of them

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim theButtons(5) As Button
Dim buttonlocation As Point

For counter As Integer = 1 To 5
Dim theButton As New Button
theButton.Name = "Btn" & counter.ToString()
theButton.Width = 26
theButton.Height = 26
buttonlocation.X = counter * 100
buttonlocation.Y = 100
theButton.Location = buttonlocation

theButton.Text = counter.ToString()
theButtons(counter) = theButton

Me.Controls.Add(theButton) '<--- Add to the forms collection

Next

End Sub


Private Sub ButtonRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRemove.Click
For i = 2 To 4
Me.Controls.RemoveByKey("Btn" & i.ToString)
Next
End Sub
End Class





Re: Visual Basic General Deleting screen objects programmatically

KAllbritain

thanks spotty