473,753 Members | 6,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3451
Hi Ron,

Try something like this...

First of all, when creating the nodes, attach the contextmenustri p to
the nodes ContextMenuStri p. 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.MouseD own event. In that event and
if right mouse button is clicked, clear the contextmenustri p, 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(obje ct sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
TreeNode node = new TreeNode(i.ToSt ring());
node.Tag = i;
node.ContextMen uStrip = contextMenuStri p1;
treeView1.Nodes .Add(node);
}
}

private void treeView1_Mouse Down(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Ri ght)
{
contextMenuStri p1.Items.Clear( );
TreeNode node = ((TreeView)send er).GetNodeAt(e .Location);
if (node != null)
{
ToolStripMenuIt em gnu = new ToolStripMenuIt em("Here's the
name: " + node.Tag.ToStri ng());
gnu.Tag = node.Tag;
gnu.Click += new EventHandler(gn u_Click);
contextMenuStri p1.Items.Add(gn u);
}
}
}

void gnu_Click(objec t sender, EventArgs e)
{
MessageBox.Show (((ToolStripMen uItem)sender).T ag.ToString());
}

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**********@c onfideltial.com wrote in message
news:eO******** ******@TK2MSFTN GP02.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
1518
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 /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/3.2.2/specs Configured with: ../configure --disable-nls --with-ld=/usr/ccs/bin/ld --with-as=/usr/ccs/bin/as
3
4777
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 creation takes some time. The approach I took was to override the OnSelect method of the MenuItem class, which had code to populate the
3
1552
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 ( I have a deptmember table like this: DeptID MemberID IsManager 1 1 Y
2
1668
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 menus but the main problem is that they have to be downloaded from server after the page loads. I can't load all of them on page because there are to many. This is my solution of the problem. When user right-clicks js download from server through...
1
1550
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 the Explorer Windows opens the context menu with the option "Use My App" and selecting this runs My App with the selected file name. Just as I wanted. The problem I now have is that double clicking a Visual Studio file (.sln) also starts My...
4
1716
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 the app is started and then assign them when the object is clicked on. The first few items in the menus will all be the same (Large Icons, Small Icons, etc.). I create all of those objects, then add them to the first context menu and all is...
4
4002
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 looking for. i am looking for code to make a dynamical menu with submenus The menu items are stored in a MS SQL Table and when by loading the page the items will be created. i have made a asp table. i want my menu on the left cell, in the middle...
3
4793
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 dynamically generated, and the dynamic generation needs to occur right when the user selects this menu item (that is on the Popup event handler). However, everytime I put following code on the Popup event handler (of the View Files menuitem) to dynamically...
0
9072
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8896
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9451
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9421
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9333
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8328
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6869
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2284
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.