Treeview and ContextMenuStrip

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Cool

    #1

    Treeview and ContextMenuStrip

    Let's say I have a Treeview control on a form. Each leaf node in the
    Treeview has a ContextMenuStri p, each with one ToolStripMenuIt em, and
    all ToolStripMenuIt ems Click event is handled by a comment event
    handler.

    How do I determine which Treeview Node signalled the menu event in the
    common event handler?
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Treeview and ContextMenuStri p

    Joe,

    When creating the ContextMenuStri p for the nodes in the treeview
    (assuming you have a separate ContextMenuStri p for each node), I would
    assign the Tag property to the node in the tree view. Then, in the event
    handler, you can get the Tag property and know which node triggered the
    menu.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Joe Cool" <joecool@home.n etwrote in message
    news:317dh3hagh k3g9ckr8uokqdcb v8fe4jpv9@4ax.c om...
    Let's say I have a Treeview control on a form. Each leaf node in the
    Treeview has a ContextMenuStri p, each with one ToolStripMenuIt em, and
    all ToolStripMenuIt ems Click event is handled by a comment event
    handler.
    >
    How do I determine which Treeview Node signalled the menu event in the
    common event handler?

    Comment

    • xmail123@yahoo.com

      #3
      Re: Treeview and ContextMenuStri p

      Since you are already using a treeview maybe you can answer this
      question for me. I am new to C# and am trying to populate a treeview
      from a SQL 2005 table. Do you have s snippit of code that will do
      that or know a URL where I can see how?

      I would really appreciate the help.

      Thanks


      On Wed, 17 Oct 2007 23:37:35 GMT, Joe Cool <joecool@home.n etwrote:
      >Let's say I have a Treeview control on a form. Each leaf node in the
      >Treeview has a ContextMenuStri p, each with one ToolStripMenuIt em, and
      >all ToolStripMenuIt ems Click event is handled by a comment event
      >handler.
      >
      >How do I determine which Treeview Node signalled the menu event in the
      >common event handler?

      Comment

      • Joe Cool

        #4
        Re: Treeview and ContextMenuStri p

        On Thu, 18 Oct 2007 00:42:26 GMT, xmail123@yahoo. com wrote:
        >Since you are already using a treeview maybe you can answer this
        >question for me. I am new to C# and am trying to populate a treeview
        >from a SQL 2005 table. Do you have s snippit of code that will do
        >that or know a URL where I can see how?
        Do you know how to either a) read a SQL2005 table, or b) populate a
        treeview?
        >
        >I would really appreciate the help.
        >
        >Thanks
        >
        >
        >On Wed, 17 Oct 2007 23:37:35 GMT, Joe Cool <joecool@home.n etwrote:
        >
        >>Let's say I have a Treeview control on a form. Each leaf node in the
        >>Treeview has a ContextMenuStri p, each with one ToolStripMenuIt em, and
        >>all ToolStripMenuIt ems Click event is handled by a comment event
        >>handler.
        >>
        >>How do I determine which Treeview Node signalled the menu event in the
        >>common event handler?

        Comment

        • Phill W.

          #5
          Re: Treeview and ContextMenuStri p

          Joe Cool wrote:
          Let's say I have a Treeview control on a form. Each leaf node in the
          Treeview has a ContextMenuStri p, each with one ToolStripMenuIt em, and
          all ToolStripMenuIt ems Click event is handled by a comment event
          handler.
          >
          How do I determine which Treeview Node signalled the menu event in the
          common event handler?
          The ContextMenuStri p will be passed as the "sender" argument to the
          event handler, as in

          Sub xyz_Click( sender as Object, e as EventArgs )

          Dim cms as ContextMenuStri p _
          = DirectCast( sender, ContextMenuStri p )

          From that, you can get the control that caused the ContextMenuStri p to
          be shown, which will either be the TreeView or the TreeNode (I can't
          remember which), as in

          either
          Dim tn as TreeNode _
          = cms.SourceContr ol
          or
          Dim tn as TreeNode _
          = DirectCast(cms. SourceControl,T reeView).Select edNode

          HTH,
          Phill W.

          Comment

          • xmail123@yahoo.com

            #6
            Re: Treeview and ContextMenuStri p

            I can connect to the DB, create the dataadapter, populate a data set.
            And I know how to add nodes to a treeview. The problem is how to use
            the dataset to populate a treeview.

            I have not found a Windows Forms C# example that can handle an
            unspecified level of node nesting and Parent = Child. See child rows
            15.

            I would really appreciate the help.

            Thank you.

            I have a SQL Server 2005 table containing this data shown below.


            Child...Parent. ..Depth.....Hie rarchy
            1.........NULL. ....0.........0 1
            2..........1... ....1.......... 01.02
            5..........2... ....2.......... 01.02.05
            6..........2... ....2.......... 01.02.06
            3..........1... ....1.......... 01.03
            7..........3... ....2.......... 01.03.07
            11.........7... ....3.......... 01.03.07.11
            14.........11.. ....4.......... 01.03.07.11.14
            12.........7... ....3.......... 01.03.07.12
            13.........7... ....3.......... 01.03.07.13
            8..........3... ....2.......... 01.03.08
            9..........3... ....2.......... 01.03.09
            4..........1... ....1.......... 01.04
            10.........4... ....2.......... 01.04.10
            15.........NULL ....0.......... 15
            15.........15.. ....1.......... 15.15
            16.........15.. ....1.......... 15.16
            18.........16.. ....2.......... 15.16.18
            17.........15.. ....1.......... 15.17

            On Thu, 18 Oct 2007 00:59:59 GMT, Joe Cool <joecool@home.n etwrote:
            >On Thu, 18 Oct 2007 00:42:26 GMT, xmail123@yahoo. com wrote:
            >
            >>Since you are already using a treeview maybe you can answer this
            >>question for me. I am new to C# and am trying to populate a treeview
            >>from a SQL 2005 table. Do you have s snippit of code that will do
            >>that or know a URL where I can see how?
            >
            >Do you know how to either a) read a SQL2005 table, or b) populate a
            >treeview?
            >
            >>
            >>I would really appreciate the help.
            >>
            >>Thanks
            >>
            >>
            >>On Wed, 17 Oct 2007 23:37:35 GMT, Joe Cool <joecool@home.n etwrote:
            >>
            >>>Let's say I have a Treeview control on a form. Each leaf node in the
            >>>Treeview has a ContextMenuStri p, each with one ToolStripMenuIt em, and
            >>>all ToolStripMenuIt ems Click event is handled by a comment event
            >>>handler.
            >>>
            >>>How do I determine which Treeview Node signalled the menu event in the
            >>>common event handler?

            Comment

            • Jack Jackson

              #7
              Re: Treeview and ContextMenuStri p

              I assume that the records you get back from the dataset have the child
              records after their parent. If not, then the process becomes much
              more complicated.

              The brute force method is, for each record, loop through the treeview
              nodes until you find the parent and add the new node.

              If the Child field was unique, then you could use Child as the key for
              each node. That way you could loop through the records in the
              dataset, directly locate the parent using the Parent value as the key,
              and add the new node. Yours are not unique, so unless you can modify
              the data this technique will not work for you. I'm not sure exactly
              what your data looks like, but maybe you can do this if you make the
              key be a combination of Child and Depth if that combination is unique.


              On Mon, 22 Oct 2007 14:24:54 GMT, xmail123@yahoo. com wrote:
              >I can connect to the DB, create the dataadapter, populate a data set.
              >And I know how to add nodes to a treeview. The problem is how to use
              >the dataset to populate a treeview.
              >
              >I have not found a Windows Forms C# example that can handle an
              >unspecified level of node nesting and Parent = Child. See child rows
              >15.
              >
              >I would really appreciate the help.
              >
              >Thank you.
              >
              >I have a SQL Server 2005 table containing this data shown below.
              >
              >
              >Child...Parent ...Depth.....Hi erarchy
              >1.........NULL .....0......... 01
              >2..........1.. .....1......... .01.02
              >5..........2.. .....2......... .01.02.05
              >6..........2.. .....2......... .01.02.06
              >3..........1.. .....1......... .01.03
              >7..........3.. .....2......... .01.03.07
              >11.........7.. .....3......... .01.03.07.11
              >14.........11. .....4......... .01.03.07.11.14
              >12.........7.. .....3......... .01.03.07.12
              >13.........7.. .....3......... .01.03.07.13
              >8..........3.. .....2......... .01.03.08
              >9..........3.. .....2......... .01.03.09
              >4..........1.. .....1......... .01.04
              >10.........4.. .....2......... .01.04.10
              >15.........NUL L....0......... .15
              >15.........15. .....1......... .15.15
              >16.........15. .....1......... .15.16
              >18.........16. .....2......... .15.16.18
              >17.........15. .....1......... .15.17
              >
              >On Thu, 18 Oct 2007 00:59:59 GMT, Joe Cool <joecool@home.n etwrote:
              >
              >>On Thu, 18 Oct 2007 00:42:26 GMT, xmail123@yahoo. com wrote:
              >>
              >>>Since you are already using a treeview maybe you can answer this
              >>>question for me. I am new to C# and am trying to populate a treeview
              >>>from a SQL 2005 table. Do you have s snippit of code that will do
              >>>that or know a URL where I can see how?
              >>
              >>Do you know how to either a) read a SQL2005 table, or b) populate a
              >>treeview?
              >>
              >>>
              >>>I would really appreciate the help.
              >>>
              >>>Thanks
              >>>
              >>>
              >>>On Wed, 17 Oct 2007 23:37:35 GMT, Joe Cool <joecool@home.n etwrote:
              >>>
              >>>>Let's say I have a Treeview control on a form. Each leaf node in the
              >>>>Treeview has a ContextMenuStri p, each with one ToolStripMenuIt em, and
              >>>>all ToolStripMenuIt ems Click event is handled by a comment event
              >>>>handler.
              >>>>
              >>>>How do I determine which Treeview Node signalled the menu event in the
              >>>>common event handler?

              Comment

              • =?Utf-8?B?dHJlZXZpZXc=?=

                #8
                RE: Treeview and ContextMenuStri p

                can u send the sample code to create different contextmenu for different
                levels in a treeview...

                Comment

                Working...