Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. TreeView/ListView item selection and context menu

TreeView/ListView item selection and context menu

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EnkelIk
    wrote on last edited by
    #1

    Hi all. I have a feeling there is a very simple solution to my problem, but I just can't figure it out. Here goes: I have a form with a treeview (and a listview with the same problem), where I present different items. I have a context menu connected to the view giving the user the option to, for instance, edit or delete the selected item. The problem is that if the user has selected an item and then right clicks on another item and choose to edit, it's the first item that 'opens' to be edited. I would want the right click to FIRST select the item that was being clicked and THEN popping up the context menu so that the user edits or deletes the item that was being right clicked. Any tips? Thanks /EnkelIk

    P A H 3 Replies Last reply
    0
    • E EnkelIk

      Hi all. I have a feeling there is a very simple solution to my problem, but I just can't figure it out. Here goes: I have a form with a treeview (and a listview with the same problem), where I present different items. I have a context menu connected to the view giving the user the option to, for instance, edit or delete the selected item. The problem is that if the user has selected an item and then right clicks on another item and choose to edit, it's the first item that 'opens' to be edited. I would want the right click to FIRST select the item that was being clicked and THEN popping up the context menu so that the user edits or deletes the item that was being right clicked. Any tips? Thanks /EnkelIk

      P Offline
      P Offline
      petst
      wrote on last edited by
      #2

      MS article for VB but it should be easy. http://support.microsoft.com/kb/811399/EN-US/[^]

      1 Reply Last reply
      0
      • E EnkelIk

        Hi all. I have a feeling there is a very simple solution to my problem, but I just can't figure it out. Here goes: I have a form with a treeview (and a listview with the same problem), where I present different items. I have a context menu connected to the view giving the user the option to, for instance, edit or delete the selected item. The problem is that if the user has selected an item and then right clicks on another item and choose to edit, it's the first item that 'opens' to be edited. I would want the right click to FIRST select the item that was being clicked and THEN popping up the context menu so that the user edits or deletes the item that was being right clicked. Any tips? Thanks /EnkelIk

        A Offline
        A Offline
        afinnell
        wrote on last edited by
        #3

        It will require you to write code to select the node when the user right-clicks. It is fairly easy to write and I believe CodeProject has some examples of this. Search the .NET Controls section. The default controls don't support the feature you want. - Drew

        1 Reply Last reply
        0
        • E EnkelIk

          Hi all. I have a feeling there is a very simple solution to my problem, but I just can't figure it out. Here goes: I have a form with a treeview (and a listview with the same problem), where I present different items. I have a context menu connected to the view giving the user the option to, for instance, edit or delete the selected item. The problem is that if the user has selected an item and then right clicks on another item and choose to edit, it's the first item that 'opens' to be edited. I would want the right click to FIRST select the item that was being clicked and THEN popping up the context menu so that the user edits or deletes the item that was being right clicked. Any tips? Thanks /EnkelIk

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          This is a problem with your code that can easily be solved. Right-clicking a node should NOT select it. This is counter-intuitive as it does not follow the Windows UI Guidelines that exist to present a common user interface experience. If you change the behavior your liable to confuse users who are used to the way "everything else works". Make sure that either tie a ContextMenu to a node by referencing a particular ContextMenu (presumably, certain types of nodes all reference the same instance of a ContextMenu to save resources) by assigning an instance to the TreeNode.Tag property. Conversely, you could implement the popup of specific ContextMenus based on data in the TreeNode (like that from the Text or Tag properties; or perhaps you extend TreeNode with your own derivative). Leave TreeView.ContextMenu unassigned. Instead, handle the MouseUp event (or, if you're extending TreeView, override the OnMouseUp protected method for better control and faster execution; just be sure to call base.OnMouseUp). This gives you translated mouse coordinates (to the client, or control) that you can use in a call to TreeView.GetNodeAt:

          protected override void OnMouseUp(MouseEventArgs e)
          {
          base.OnMouseUp(e);
           
          Point pt = new Point(e.X, e.Y);
          TreeNode node = GetNodeAt(pt);
          if (node != null)
          {
          ContextMenu cm = node.Tag as ContextMenu;
          if (cm != null)
          cm.Show(this, pt);
          }
          }

          There's an example of getting the reference to the ContextMenu you want assigned to the TreeNode.Tag property. Again - reuse references to distinct ContextMenus so you don't hot system resources and otherwise frustrate the user when you start getting lots of tree nodes or have large context menus. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups