How to use contextMenuStripto remove linklable
I have created linklables dynamically and now i want to remove any of them by right clicking on that perticular linklable. I have tried it 开发者_开发知识库using context menu strip but i was unable to remove any linklable. Please help me on this.
thanks in advance
Did you try handling the MouseDown event and getting the LinkLabel from the sender argument to the event handler? From there you should be able to remove it from its parent's Controls collection.
If you want to have the ContextMenuStrip for user interaction, you can still handle the MouseDown event of the LinkLabel and store the last clicked instance in the Tag property of the ContextMenuStrip. Then, when your menu item is clicked you can retrieve the last clicked LinkLabel and proceed to remove it from the controls collection.
private void removeToolStripMenuItem_Click(object sender, EventArgs e)
{
var lbl = this.contextMenuStrip1.Tag as LinkLabel;
if (lbl != null)
lbl.Parent.Controls.Remove(lbl);
}
private void linkLabel1_MouseDown(object sender, MouseEventArgs e)
{
this.contextMenuStrip1.Tag = sender;
}
精彩评论