473,434 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

One ContextMenu assigned to many controls - which control triggered it?

Hi,

I have a single context menu assigned to 10 controls (DataGrids) on a form.

In the context menu item click event I need to determine which control
(DataGrid) is invoking this menu item. Something like this:

private void cmnuItem1_Click(object sender, System.EventArgs e)
{
string sInvokingControlName = ..... which DataGrid invoked the context
menu .....

switch(sInvokingControlName)
{
case "DataGrid1"
.... run some operation on datagrid 1
break;
case "DataGrid2"
.... run some operation on datagrid 2
break;
case "DataGrid3"
.... run some operation on datagrid 3
break;
}
}
Any suggestions how I could achieve this?

Thanks,
PeterZ
Nov 16 '05 #1
2 2090
Use the sender:

private void cmnuItem1_Click(object sender, System.EventArgs e)
{
Control ctrl = (Control) sender;
string sInvokingControlName = ctrll.Name
.....

}

"PeterZ" <_r**********************@hotmail.com> wrote in message
news:cr***********@otis.netspace.net.au...
Hi,

I have a single context menu assigned to 10 controls (DataGrids) on a
form.

In the context menu item click event I need to determine which control
(DataGrid) is invoking this menu item. Something like this:

private void cmnuItem1_Click(object sender, System.EventArgs e)
{
string sInvokingControlName = ..... which DataGrid invoked the context
menu .....

switch(sInvokingControlName)
{
case "DataGrid1"
.... run some operation on datagrid 1
break;
case "DataGrid2"
.... run some operation on datagrid 2
break;
case "DataGrid3"
.... run some operation on datagrid 3
break;
}
}
Any suggestions how I could achieve this?

Thanks,
PeterZ

Nov 16 '05 #2
Thanks uebercoder.

As I found out, 'sender' is always going to give me the MenuItem control
itself. But it was enough to get me thinking and expand a little further to
get what I want (the control which triggered the context menu).....
private void cmnuItem1_Click(object sender, System.EventArgs e)
{
// Get the control which triggered the context menu.
MenuItem mi = (MenuItem)sender;
ContextMenu cm = mi.GetContextMenu();
Control control = cm.SourceControl;

if (control is DataGrid)
{
string sInvokingControlName = control.Name
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mike Allen | last post by:
Hi Everyone, My problem concerns ContextMenus. It's easy enough to create one and apply it to a NotifyIcon. When it's compiled and run, it works beautifully. Right click and there you are. But I...
0
by: MD | last post by:
I have a ContextMenu that is assigned to 3 RichTextBox's (i.e., if you right-click on any of the 3 RichTextBox's, you get the same ContextMenu). My ContextMenu has 2 items, each of which have their...
2
by: VR | last post by:
I have a form with a list view control, a button control and a main menu with menuDelete item. Any time I am handling a button click event or menuDelete click event I call DeleteItem() function...
0
by: Jorge | last post by:
Hello I am trying to browse a form and i know it has 3 contextmenus and a mainmenu. Public Sub PercorreMenus(ByVal f As Form) Dim ctl As Control For Each ctl In f.Controls If...
4
by: Frank Rizzo | last post by:
Hello, I have a listview control with bunch of entries. When the user right-clicks on the item, I want a context menu to come up. So I created a ContextMenu instance and assigned it to...
11
by: Graham Charles | last post by:
I'm writing a control inheriting from ComboBox, and I'd like to add an entry to the ContextMenu for that combo box. I have no problem if I create an entirely new ContextMenu from scratch, but I'd...
7
by: wandii | last post by:
Hi, I am trying to show the shortcut menu on a richtextbox control by adding the contextmenu component onto the form. The richtextbox property is assigned to the contextmenu. I created my...
0
by: Paul Cheetham | last post by:
Hi, I have a strange problem in one of my applications. It occurs after I show a second form from my main form. When I close the second form, and re-show the main form I sometimes get an...
1
by: Darin | last post by:
I understand microsoft's change from contextmenu to contextmenustrip - the strip looks "prettier". But, since the contextmenu isn't available in the designer, it suer woul dhave been nice when...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.