473,395 Members | 1,675 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,395 software developers and data experts.

Build dynamic context menus for trees

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
Oct 30 '06 #1
2 3409
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.To String());
}

Ron M. Newman wrote:
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
Oct 30 '06 #2
You must write the code on mouse down event.

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

"Ron M. Newman" <co**********@confideltial.comwrote in message
news:eO**************@TK2MSFTNGP02.phx.gbl...
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

Nov 9 '06 #3

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

Similar topics

0
by: matt okeson-harlow | last post by:
i am getting errors trying to build MySQLdb on solaris 8, i have tried changing the libs, no joy, anyone have any ideas? Python 2.3.4 gcc -v Reading specs from...
3
by: Eddie de Bear | last post by:
Hi, A project I am working on has a requirement for dynamic menus. For the most part this works really well. The menus I'm creating a based on files and directories, so naturally the menu...
3
by: HL5138 | last post by:
I have a department table like this: DeptID Department ParentID, Lineage 1 HR NULL ( 2 Temp1 1 (1, 3 Temp2 2 (1,2 4 PC NULL ...
2
by: Patryk | last post by:
Hi I'm making a web application which is a copy of existing desktop application. I use telerik controlls for MS visual studio I have a little bit complicated situation beacuse I need context...
1
by: Doug Bell | last post by:
Hi, If I modify my Registry adding a new key HKEY_CLASSES_ROOT\*\shell\Use My App\command with a (Default) Value of C:\Program Files\Doug\DotNetApps\MyApp.exe "%1" then right clicking on files in...
4
by: ssg31415926 | last post by:
I've got a ListView which can show many different types of object. I need to display a ContextMenu whose MenuItems depend on the object type. I was planning to pre-create the ContextMenus when...
4
by: paula | last post by:
I've got a problem with asp.net i am trying to make a menu control. and have searched the web for serveral controls but they don't work correctly. I am pretty new to asp.net building. What am i...
3
by: RahimAsif | last post by:
I am writing an application that requires the a portion of the main menu to be dynamic. The menu has file, panels, view files and help across the top. The view files sub menu needs to be...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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...

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.