Click to See Complete Forum and Search --> : Removing controls
Jym
October 20th, 2002, 09:53 AM
Hi ...
Here's what I have ... Panel1 which I have used
me.panel1.controls.add(lbl) to add 10 labels to panel1
now I am trying to get rid of them using
for x = 1 to 10
me.panel1.controls.remove(controls(0))
Next
and though it runs the code 10 times, all of the controls are still there, please let me know what I am doing wrong :)
Thanks
Athley
October 20th, 2002, 10:21 AM
You have to get a reference to the object to be deleted. If you do like this it will work:
Dim lbl As Label
lbl.Name = "TestLabel"
Me.Controls.Add(lbl)
.....
Me.Controls.Remove(lbl)
.... because you still have the variable lbl referenced to the added control.
So you have to do something like this:
Dim con As control
For Each lbl In Me.Controls
If con.Name = "TestLabel" then Me.Controls.Remove(con)
Next
I think you get it. Bottom line: Either way you choose to do this you have to get a reference to the object being deleted.
/Leyan
Athley
October 20th, 2002, 10:23 AM
of course it should be:
For Each con In Me.Controls
sorry. :cool:
/Leyan
Jym
October 20th, 2002, 10:42 AM
ok I have tried this and I'm still not removing labels
in my on_click for add a line of labels
it gets all the text, and positions of a label then goto addlabel
private sub addlabel
Dim lbl As New Label()
'for the long box which will equal the control count
lbl.Width = w
lbl.Name = "whatever"
lbl.BackColor = Me.BackColor
lbl.Text = LabelText
lbl.Height = 14
lbl.TextAlign = ta
lbl.Anchor = Label9.Anchor
lbl.Left = l
lbl.Top = distance
lbl.Visible = True
Me.Panel1.Controls.Add(lbl)
end sub
it does this 8 times to add the 8 new labels of the line
Then when someone clicks Delete Line I use the code
dim cntl as Control
dim counter as integer
cntl = Me.Panel1.Controls(0)
For counter = 1 To 8
If cntl.Name = "whatever" Then Me.Panel1.Controls.Remove(cntl)
end if
next
exit sub
and the labels are still there. I've tried probably 60 different interprutations of that remove line and still the labels are still there :(
Jym
October 20th, 2002, 10:56 AM
ok attempt number 3002 I have done this and it seems to work
for counter = 1 to 8
for each cntl in me.panel1.controls
if cntl is me.panel1.controls(0) then me.panel1.controls.remove(cntl)
next
next
is that good coding practice ? it seems so ... complicated for such an easy task :)
Athley
October 20th, 2002, 10:57 AM
Well, as I see it, it's because cntl does not get a reference to the control. Do you have to use a standard For-loop?
I had the following code in one button.....
Dim lbl As New Label()
lbl.Location = New System.Drawing.Point(20, 25)
Me.Panel1.Controls.Add(lbl)
.....and the following code in another button......
Dim pan As Panel = Me.Panel1
Dim con As Control
For Each con In pan.Controls
If con.GetType.ToString = "System.Windows.Forms.Label" Then
pan.Controls.Remove(con)
End if
Next
.... and this removes all labels added by the first button.
/Leyan
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.