删除动态创建的控件C# [英] removing dynamically created controls C#
问题描述
我有一个程序,增加了一系列的光点的一个图。我怎么有一个按钮清除所有已与该code创建的光点的?
I have a program that adds a series of "blips" to a graph. How do I have a button clear all of the "blips" that have been created with this code?
PictureBox blip = new PictureBox();
blip.Location = new Point(blipHours, blipAltitude);
blip.Size = new Size(6, 6);
blip.BackColor = System.Drawing.Color.Lime;
blip.Text = "";
blip.Name = callsign;
this.Controls.Add(blip);
this.Controls.SetChildIndex(blip, 0);
我的问题的另一部分:看到我命名为每昙花一现的呼号。有没有办法来改变昙花一现的背景颜色时,它的名字是等于特定的呼号?每一个光点与在列表框中选择相关的,我想拥有它改变光点的颜色,当用户选择它。
The other part of my question: See that I have named each blip with "callsign." Is there a way to change a blip's background color when it's name is equal to a certain callsign ? Each blip is associated with a selection in a listbox, and I would like have it change the blips color when that user selects it.
推荐答案
每个人都忘记了一个的非常的重要细节:你要的Dispose()的控制,否则将永远泄漏:
Everybody is forgetting a very important detail: you have to Dispose() the control or it will leak forever:
for (int ix = this.Controls.Count - 1; ix >= 0; ix--)
if (this.Controls[ix] is PictureBox) this.Controls[ix].Dispose();
这篇关于删除动态创建的控件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!