Context menus and treeviews, suggestions for a NODE sensitive context menu

E

Eric Newton

Easy part is creating a context menu for a specific node

(one of several examples follows)
Private Sub treeview_MouseUp(byval sender as object, byval e as
MouseEventArgs) handles TreeView1.MouseUp
if e.Button = MouseButtons.Right then
Dim node as TreeNode = TreeView1.GetNodeAt(e.X,e.Y)
if not node is nothing then
if typeof node is TYPE1 then

ShowContextMenu(BuildContextMenu(DirectCast(node.Tag,TYPE1)))
elseif typeof node is TYPE2 then

ShowContextMenu(BuildContextMenu(DirectCast(node.Tag,TYPE2)))
'... and so on ...
end if
end if
end if
end if

NOW Heres the problem:

Once a menu item is clicked, how does one accurately get the node that
triggered the whole set of operations?

Answer is, you cant... since MenuItems dont have TAGS you cannot associate
Clicks events with actual data.

A serious flaw in the MenuItem class in my opinion
 
I

Iulian Ionescu

You can inherit the MenuItem class and add the Tag
property... It will work very well...
 
H

Herfried K. Wagner [MVP]

* "Eric Newton said:
Easy part is creating a context menu for a specific node

(one of several examples follows)
Private Sub treeview_MouseUp(byval sender as object, byval e as
MouseEventArgs) handles TreeView1.MouseUp
if e.Button = MouseButtons.Right then
Dim node as TreeNode = TreeView1.GetNodeAt(e.X,e.Y)
if not node is nothing then
if typeof node is TYPE1 then

ShowContextMenu(BuildContextMenu(DirectCast(node.Tag,TYPE1)))
elseif typeof node is TYPE2 then

ShowContextMenu(BuildContextMenu(DirectCast(node.Tag,TYPE2)))
'... and so on ...
end if
end if
end if
end if

NOW Heres the problem:

Once a menu item is clicked, how does one accurately get the node that
triggered the whole set of operations?

Answer is, you cant... since MenuItems dont have TAGS you cannot associate
Clicks events with actual data.

You may want to inherit from 'MenuItem' and extend it by a 'Tag'
property. Notice that this will break compatibility with the menu
designer.
 
D

Doug Forster

Hi Eric,

Did you read my response to the same question on 24/12 ?

Cheers

Doug Forster
 
E

Eric Newton

actually i had not seen your post, which is bizarre cause i usually "have
Show Replies to my messages" checked...

Anyways, I liked your suggestion, however, it just seems like the context
menu should probably remember this itself, and be able to present it to
Click events

or better yet, MenuItems have a Select event (or whatever it wouldbe called)
with a lot more info in the EventArgs

thanks though, Doug
--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

Doug Forster said:
Hi Eric,

Did you read my response to the same question on 24/12 ?

Cheers

Doug Forster

Eric Newton said:
Easy part is creating a context menu for a specific node

(one of several examples follows)
Private Sub treeview_MouseUp(byval sender as object, byval e as
MouseEventArgs) handles TreeView1.MouseUp
if e.Button = MouseButtons.Right then
Dim node as TreeNode = TreeView1.GetNodeAt(e.X,e.Y)
if not node is nothing then
if typeof node is TYPE1 then

ShowContextMenu(BuildContextMenu(DirectCast(node.Tag,TYPE1)))
elseif typeof node is TYPE2 then

ShowContextMenu(BuildContextMenu(DirectCast(node.Tag,TYPE2)))
'... and so on ...
end if
end if
end if
end if

NOW Heres the problem:

Once a menu item is clicked, how does one accurately get the node that
triggered the whole set of operations?

Answer is, you cant... since MenuItems dont have TAGS you cannot associate
Clicks events with actual data.

A serious flaw in the MenuItem class in my opinion


--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top