Click here to Skip to main content
15,918,343 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I want to use a single ContextMenuStrip for different edit boxes (or text labels). The ContextMenuStrip is simply containing Edit and Remove entries and I want to Edit or Remove the corresponding data from my application. I have written the relevant code but how could I found that the context menu is clicked from which text box (or text label)? Can any one help me ?

This discussion[^] could help you.
Share this answer
Mushtaq Muhammad 10-Apr-11 10:21am    
Working. Thnaks
Abhinav S 10-Apr-11 10:22am    
You are welcome.
Sergey Alexandrovich Kryukov 10-Apr-11 22:31pm    
Sure, it should work, my 5.
Abhinav S 11-Apr-11 0:19am    
Thank you SA.
As with all other control events, the first parameter is "sender".
If you cast this to a ContextMenuStrip control, then the SourceControl property is the control that was right clicked to give you the menu box:
private void contextMenuStrip1_Click(object sender, EventArgs e)
    ContextMenuStrip cms = sender as ContextMenuStrip;
    if (cms != null)

Do note that the SourceControl is the highest level you have associated the menu with: if you have associated the ContextMenuStrip with the form only, then it will always be the form as the SourceControl. You have to specify the ContextMenuStrip as the Control.ContextMenuStrip property of each control individually!
Share this answer
Mushtaq Muhammad 10-Apr-11 10:24am    
in this case, cms is always a null. The solution suggested by Abhinav S is working as follows

ToolStripMenuItem tsItem = (ToolStripMenuItem)sender;
ContextMenuStrip cMenuStrip = (ContextMenuStrip)tsItem.Owner;
Control theSourceControl = cMenuStrip.SourceControl;

Any way, thanks for your cooperation.
Sergey Alexandrovich Kryukov 10-Apr-11 22:31pm    
The idea is correct, but the type of sender is different.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900