vb.net - How to remove dynamically created controls? -
i've added few pictureboxes form with:
at top of code:
private flag picturebox under form1_load event:
flag.location = new point(buttonx, buttony) flag.backcolor = color.red flag.borderstyle = borderstyle.fixedsingle flag.name = "flag" & pos me.tabpage1.controls.add(flag) flag.bringtofront() so end with, example, 4 pictureboxes named "flag2" "flag5" "flag6" "flag8". try remove them with:
dim con control each con in me.tabpage1.controls if con.name.contains("flag") me.tabpage1.controls.remove(con) end if next what ends happening not pictureboxes deleted. if run delete code multiple times rid of them i'm trying remove them @ once. tried:
dim con control dim integer = 0 while < me.tabpage1.controls.count con = me.tabpage1.controls(i) if con.name.contains("flag") me.tabpage1.controls.remove(con) end if += 1 end while both "delete codes" delete picture boxes in same order each other (not in order created way) instead of deleting of picture boxes.
try this:
'create pbs delete dim pbs = me.tabpage1.controls.oftype(of picturebox)(). where(function(pb) pb.name.contains("flag")) 'loop thru them each pb in pbs me.tabpage1.controls.remove(pb) next sub:
public sub removepictureboxes(tp tabpage) 'create pbs delete dim pbs = tp.controls.oftype(of picturebox)(). where(function(pb) pb.name.contains("flag")) 'loop thru them each pb in pbs tp.controls.remove(pb) next end sub usage:
removepictureboxes(tabpage1)
Comments
Post a Comment