-
February 4th, 2009, 08:51 AM
#1
[RESOLVED] Adding EventHandlers to a Dynamic ContextMenuStrip
Hello all,
First off, I am fairly new to this, so please forgive me if this turns out to be a fairly "basic" question.
I am creating a ContextMenuStrip for a NotifyIcon, which is populated by a list of servers at runtime in an XML document on our network. That works fine. I am having difficulty in adding the click event handlers for those dynamically added items. I have found examples around on the web, but they were either incomplete snippets or I am missing something else, such as a declaration or something. Here is the code for the Context Menu:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace MKB_Remote_Desktop
{
public partial class MKBRemoteDesktopForm : Form
{
public MKBRemoteDesktopForm()
{
InitializeComponent();
populateMenu();
}
public void populateMenu()
{
mnuContext.Items.Clear();
mnuContext.Items.Add("Exit");
mnuContext.Items.Add("-");
mnuContext.Items.Add("Configuration");
mnuContext.Items.Add("Refresh");
mnuContext.Items.Add("-");
string xmlsvrname;
XmlTextReader readXml = new XmlTextReader("\\\\***\\servers.xml");
readXml.Read();
while (readXml.Read())
{
switch (readXml.NodeType)
{
case XmlNodeType.Element:
break;
case XmlNodeType.Text:
xmlsvrname = readXml.Value;
mnuContext.Items.Add(xmlsvrname); //Need handler for these items
break;
case XmlNodeType.EndElement:
break;
}
}
}
}
}
I have been trying to figure out:
mnuContext.Items.Add(xmlsvrname, new EventHandler..(Don't know what at put here));
But I am not sure what to reference..
Thanks in advance,
Jack V
EDIT: Sorry - posting the message obliterated the indentations in the code. Added code tag
EDIT2: The ContextMenuStrip (mnuContext) itself is not dynamically generated. I added it via the design editor.
Last edited by jrv; February 4th, 2009 at 04:57 PM.
-
February 4th, 2009, 09:35 AM
#2
Re: Adding EventHandlers to a Dynamic ContextMenuStrip
I figured it out -
I defined the eventhandler using:
Code:
mnuContext.ItemClicked += new ToolStripItemClickedEventHandler(mnuContext_ItemClicked);
then wrote the handler as:
Code:
void mnuContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "Exit")
{
Application.Exit();
}
}
(As a simple example).
Hope this helps anyone else with the same problem.
I was confused because most of the examples on the web still use the older ContextMenu, not the ContextMenuStrip, which has some different properties.
-
February 4th, 2009, 01:47 PM
#3
Re: Adding EventHandlers to a Dynamic ContextMenuStrip
Another thing to consider is nearly every object in Visual C# has a .Tag property that can be used to store pretty much anything you want. This allows you to have a programmatically created button with .Text = "Server_01" and the .Tag = "172.20.20.20" (just a random idea).
-
February 4th, 2009, 04:56 PM
#4
Re: Adding EventHandlers to a Dynamic ContextMenuStrip
I was not aware of the .Tag property, thank you very much.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|