【发布时间】:2014-05-12 05:51:01
【问题描述】:
我正在动态绘制一组标签,我想删除它们。我正在使用相同的按钮来删除和添加新标签。标签是使用随机坐标随机绘制的。但是,当我按下按钮时,我应该删除旧标签并出现新标签。但是,我所拥有的是新标签出现,旧标签出现但空标签。我希望它们被完全删除。看图:
//Global Intialization
int xCoor;
int yCoor;
//send the random method
Random coor = new Random();
private void btnRun_Click(object sender, EventArgs e)
{
//Removing the Labels after drawing them in the picBox
this.RemoveOldLabels();
//to draw the Labels rendomely.
for (int x = 1; x <= 25; x++)
{
for (int i = 0; i < 1; i++)
{
//Get the Coordinates for X,Y
xCoor = coor.Next(0, 750);
yCoor = coor.Next(0, 500);
//Start Greating the Labels
Label nodeLabel1 = new Label();
nodeLabel1.Text = x + " : " + xCoor + "," + yCoor;
nodeLabel1.AutoSize = true;
nodeLabel1.Location = new Point(xCoor + 10, yCoor + 5);
nodeLabel1.ForeColor = System.Drawing.Color.Red;
nodeLabel1.BackColor = Color.LightBlue;
//Draw the Labels in the PicBox
this.picNodes.Controls.Add(nodeLabel1);
}
}
}
//this to remove the Labels
private void RemoveOldLabels()
{
List<Label> LabelsToRemove = new List<Label>();
foreach (var x in this.picNodes.Controls)
{
if (x.GetType() == typeof(System.Windows.Forms.Label))
{
LabelsToRemove.Add((Label)x);
}
}
【问题讨论】:
-
为什么不创建一个标签列表并将创建的标签添加到列表中,然后在 RemoveOldLabels 中将它们全部删除?
-
因为我以后会有随机数的标签。
-
我不明白 - 你的意思是你的名字中会有随机数字还是......?
-
我每次都会有随机数量的标签。