Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / November 2006

Tip: Looking for answers? Try searching our database.

Build dynamic context menus for trees

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ron M. Newman - 30 Oct 2006 19:15 GMT
Hi,

Just need a little advice. Id like to build *dynamic* context menus for tree
nodes. I'm pretty versed in building context menus and attaching them to
tree nodes.

My question is, what event to I "capture" in order to build the tree node
menu in real time? right click on a tree node? or is it too late?

just FYI: the menu is different for each node and is based on "real time"
parameters. and there's no real way to prepare it in advance.

Thanks
Ron
christer.dk@gmail.com - 30 Oct 2006 22:07 GMT
Hi Ron,

Try something like this...

First of all, when creating the nodes, attach the contextmenustrip to
the nodes ContextMenuStrip. You also need information about what the
treenode represents. Example: Maybe you want to display different
context menu options based on the users rights to the represented
resources. You can store an identifier to each resource in the Tag
property of the corresponding treenode, or even more information.
Consider performance penalties on loading information at treenode
creation time (up front) or at context menu creation time.

Add an eventhandler to the TreeView.MouseDown event. In that event and
if right mouse button is clicked, clear the contextmenustrip, fetch the
clicked node and get the information stored in the Tag propery. Check
for null reference on the node (if the user right clicks, but not on a
node...). Create the menu items, add click event handler, and attach
the Tag value to the menu items (so that you have access to that
information when the user invokes the click event).

See included code snippets below.

This should cover your runtime context menu needs ...

Was this helpful?

Kind regards,
Christer

private void Form1_Load(object sender, EventArgs e)
{
   for (int i = 0; i < 5; i++)
   {
       TreeNode node = new TreeNode(i.ToString());
       node.Tag = i;
       node.ContextMenuStrip = contextMenuStrip1;
       treeView1.Nodes.Add(node);
   }
}

private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
   if (e.Button == MouseButtons.Right)
   {
       contextMenuStrip1.Items.Clear();
       TreeNode node = ((TreeView)sender).GetNodeAt(e.Location);
       if (node != null)
       {
           ToolStripMenuItem gnu = new ToolStripMenuItem("Here's the
name: " + node.Tag.ToString());
           gnu.Tag = node.Tag;
           gnu.Click += new EventHandler(gnu_Click);
           contextMenuStrip1.Items.Add(gnu);
       }
   }
}

void gnu_Click(object sender, EventArgs e)
{
   MessageBox.Show(((ToolStripMenuItem)sender).Tag.ToString());
}

> Hi,
>
[quoted text clipped - 10 lines]
> Thanks
> Ron
waheed iqbal - 09 Nov 2006 09:24 GMT
You must write the code on mouse down event.

Identify the Tree Node by custom code and then perform you task.

> Hi,
>
[quoted text clipped - 10 lines]
> Thanks
> Ron

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.