0

我为每个PictureBox's 和's 动态创建了一些 's ,并带有一个名为“Remove”的 a ......我只想删除与被点击的那个相关的's ,但这只是附加到 all的,而不是's。LabelPictureBoxContextMenuToolStripMenuItemPictureBoxLabelPictureBoxContextMenuPictureBoxLabel

有没有办法仅根据单击的第一个控件一次单击删除两个控件?

怎么做 ?

4

2 回答 2

2

创建 PictureBox 时,在 PictureBox 的 Tag() 属性中存储对关联标签的引用:

Label lbl = new Label();
PictureBox pb = new PictureBox();
pb.Tag = lbl;

稍后您可以同时删除它们:

// ...assuming "pb" now refers to the PictureBox that fired the ContextMenu...
((Control)pb.Tag).Dispose();
pb.Dispose();
于 2013-11-11T18:12:30.890 回答
1

您可以将另一个控件的 id 作为其属性存储在第一个控件中。现在您可以使用第一个控件单击事件中的属性删除第二个控件。

于 2013-11-11T17:49:12.333 回答