delete controls

  • Thread starter Thread starter s.lustre
  • Start date Start date
S

s.lustre

after adding controls to a panel on my form, i need to delete them. does
anybody know how to programatically delete controls?

'my add function works
For i = 0 To DirectCast(dgRoutingTable.DataSource, DataView).Count()
Dim u As New RoutingControl.RoutingControl
With u
..Comments = i.ToString
..Location = New System.Drawing.Point(0, 136 * (i - 1))
End With
Me.Panel1.Controls.Add(u)
Next i


'my delete function doesnt work
For i = 0 To DirectCast(dgRoutingTable.DataSource, DataView).Count()
Dim u As RoutingControl.RoutingControl
Me.Panel1.Controls.Remove(u)
Next i
 
Your code
Dim u As RoutingControl.RoutingControl
Me.Panel1.Controls.Remove(u)
Is trying to place an uninstantiated control object to the Remove Method.

See below for an example which works.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim u As New Button
With u
.Text = "Boo"
.Name = "ThatButton"
.Location = New System.Drawing.Point(0, 0)
End With
Me.Panel1.Controls.Add(u)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

For Each c As Control In Panel1.Controls

If c.Name = "ThatButton" Then
Panel1.Controls.Remove(c)
End If

Next

End Sub

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
S lustre,

Typed in this message so try it yourself and watch typos

\\\
for i as integer = me.controls.count-1 to 0 step -1
if typeof me.controls(i) IS RoutingControl.RoutingControl then
me.controls.remove(me.controls(i))
end if
next
///
I hope this helps

Cor
 
thanks Cor and OHM
finally got it to work

For i = Me.Panel1.Controls.Count - 1 To 0 Step -1
If TypeOf Me.Panel1.Controls(i) Is RoutingControl.RoutingControl Then
Me.Panel1.Controls.Remove(Me.Panel1.Controls(i))
End If
Next
 
Back
Top