Click here to Skip to main content
16,005,222 members
Home / Discussions / C#
   

C#

 
GeneralRe: Copying List into KeyValuePair Pin
AmbiguousName10-Jun-11 0:40
AmbiguousName10-Jun-11 0:40 
GeneralRe: Copying List into KeyValuePair Pin
Pete O'Hanlon10-Jun-11 1:16
mvePete O'Hanlon10-Jun-11 1:16 
GeneralRe: Copying List into KeyValuePair Pin
BobJanova10-Jun-11 6:01
BobJanova10-Jun-11 6:01 
AnswerRe: Copying List into KeyValuePair Pin
BobJanova9-Jun-11 23:51
BobJanova9-Jun-11 23:51 
GeneralRe: Copying List into KeyValuePair Pin
AmbiguousName10-Jun-11 0:39
AmbiguousName10-Jun-11 0:39 
GeneralRe: Copying List into KeyValuePair Pin
BobJanova10-Jun-11 0:53
BobJanova10-Jun-11 0:53 
AnswerRe: Copying List into KeyValuePair Pin
Luc Pattyn10-Jun-11 0:48
sitebuilderLuc Pattyn10-Jun-11 0:48 
QuestionContext Menu Menu Item Click Event Handler Pin
namelkcip9-Jun-11 16:38
namelkcip9-Jun-11 16:38 
I have a context menu with 11 items on it, and I want to use a single event handler that will catch a click on any one of those menu items. Then I want to know which menu item was clicked and get its text value. I know how to write methods to catch the click event for each menu item individually, but surely I don't have to write a single method for each menu item click, right? This is what I know how to do for a single menu item:

private void menuItem10_Click(object sender, EventArgs e)
        {
            //Selected SOUTHERN TIER
            locationButton.Text = menuItem10.Text;
            locationButton.BackColor = System.Drawing.Color.Blue;
        }


This is what I'd like to do, but I don't know how to have a single event handler with each menu item registered to it. In other words, can I have a single event handler (we'll call it 'MenuClick') that listens for a click on any one of the menu items in the context menu? Do I have to register each one line by line or can I use a loop?

private void MenuClick(object sender, EventArgs e)
        {
            System.Windows.Forms.MenuItem clickedMenuItem = sender as System.Windows.Forms.MenuItem;
            if (clickedMenuItem != null)
            {
                // clickedMenuItem.Text
                locationButton.Text = clickedMenuItem.Text;
                locationButton.BackColor = System.Drawing.Color.Blue;
            }
        }


To register each one, it's like this:

this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);


To register each one individually seems a bit overkill and I'm looking for a loop instead. But I don't see how you can dynamically name and register a menuItem object using a variable. In other words, I don't see this as being valid:

for (i = 0; i < 12; i++)
{
     this.menuItemi.Click += new System.EventHandler(this.menuItemi_Click);
}

AnswerRe: Context Menu Menu Item Click Event Handler Pin
PIEBALDconsult9-Jun-11 18:01
mvePIEBALDconsult9-Jun-11 18:01 
AnswerRe: Context Menu Menu Item Click Event Handler Pin
Luc Pattyn10-Jun-11 0:57
sitebuilderLuc Pattyn10-Jun-11 0:57 
QuestionHow to run NetworkInterface.GetAllNetworkInterfaces over the network on a remote machine? Pin
turbosupramk39-Jun-11 11:17
turbosupramk39-Jun-11 11:17 
AnswerRe: How to run NetworkInterface.GetAllNetworkInterfaces over the network on a remote machine? Pin
Mark Salsbery9-Jun-11 12:14
Mark Salsbery9-Jun-11 12:14 
GeneralRe: How to run NetworkInterface.GetAllNetworkInterfaces over the network on a remote machine? Pin
turbosupramk39-Jun-11 12:27
turbosupramk39-Jun-11 12:27 
AnswerRe: How to run NetworkInterface.GetAllNetworkInterfaces over the network on a remote machine? Pin
Luc Pattyn9-Jun-11 12:36
sitebuilderLuc Pattyn9-Jun-11 12:36 
GeneralRe: How to run NetworkInterface.GetAllNetworkInterfaces over the network on a remote machine? Pin
turbosupramk39-Jun-11 13:48
turbosupramk39-Jun-11 13:48 
AnswerRe: How to run NetworkInterface.GetAllNetworkInterfaces over the network on a remote machine? Pin
Luc Pattyn9-Jun-11 13:59
sitebuilderLuc Pattyn9-Jun-11 13:59 
GeneralRe: How to run NetworkInterface.GetAllNetworkInterfaces over the network on a remote machine? Pin
Mark Salsbery9-Jun-11 12:42
Mark Salsbery9-Jun-11 12:42 
GeneralRe: How to run NetworkInterface.GetAllNetworkInterfaces over the network on a remote machine? Pin
turbosupramk39-Jun-11 14:15
turbosupramk39-Jun-11 14:15 
QuestionRunning .NET apps on Linux Pin
AmbiguousName9-Jun-11 3:00
AmbiguousName9-Jun-11 3:00 
AnswerRe: Running .NET apps on Linux Pin
Pete O'Hanlon9-Jun-11 3:17
mvePete O'Hanlon9-Jun-11 3:17 
AnswerRe: Running .NET apps on Linux Pin
BobJanova9-Jun-11 5:15
BobJanova9-Jun-11 5:15 
QuestionC# Windows Service Pin
scotlandc8-Jun-11 21:41
scotlandc8-Jun-11 21:41 
AnswerRe: C# Windows Service Pin
Pete O'Hanlon8-Jun-11 22:18
mvePete O'Hanlon8-Jun-11 22:18 
GeneralRe: C# Windows Service Pin
scotlandc8-Jun-11 22:22
scotlandc8-Jun-11 22:22 
AnswerRe: C# Windows Service Pin
AspDotNetDev8-Jun-11 22:46
protectorAspDotNetDev8-Jun-11 22:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.