Is there a way of getting the index of an item in a collection of ToolStripMenuItems?

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

    Is there a way of getting the index of an item in a collection of ToolStripMenuItems?

    I use to do this:

    Dim index As Integer = CType(sender, MenuItem).Index

    but ToolStripMenuIt ems do not have an Index property so I can set the Tag or
    MergeIndex property of those items and use that.

    But that is error prone since changes in the menu require that those values
    be kept synchronized.

    Even though there is no Index property I have the feeling that the
    collection might have the equivalent.



    Is there a way of getting the index of the item in the collection?





    Thanks


  • Patrice

    #2
    Re: Is there a way of getting the index of an item in a collection of ToolStripMenuIt ems?

    As in most cases the overall goal could help (and which event are you
    handling ?)

    My first thought would be first to have the event handled by the specific
    event handler tied to this control.

    If you need something more generic, my personal preference is to use the
    name or just test the object (If sender Is MyObject etc...) rather than the
    index.

    If you really need the index you'll find an IndexOf method on almost if not
    all collections (get at the owner and use the IndexOf method on the
    collection that contains these elements to get the index).
    --
    Patrice

    "Academia" <academiaNOSPAM @a-znet.coma écrit dans le message de news:
    %23ySaqDFeIHA.5 208@TK2MSFTNGP0 4.phx.gbl...
    >I use to do this:
    >
    Dim index As Integer = CType(sender, MenuItem).Index
    >
    but ToolStripMenuIt ems do not have an Index property so I can set the Tag
    or MergeIndex property of those items and use that.
    >
    But that is error prone since changes in the menu require that those
    values be kept synchronized.
    >
    Even though there is no Index property I have the feeling that the
    collection might have the equivalent.
    >
    >
    >
    Is there a way of getting the index of the item in the collection?
    >
    >
    >
    >
    >
    Thanks
    >
    >

    Comment

    • Armin Zingler

      #3
      Re: Is there a way of getting the index of an item in a collection of ToolStripMenuIt ems?

      "Academia" <academiaNOSPAM @a-znet.comschrieb
      I use to do this:
      >
      Dim index As Integer = CType(sender, MenuItem).Index
      >
      but ToolStripMenuIt ems do not have an Index property so I can set
      the Tag or MergeIndex property of those items and use that.
      >
      But that is error prone since changes in the menu require that those
      values be kept synchronized.
      >
      Even though there is no Index property I have the feeling that the
      collection might have the equivalent.
      >
      >
      >
      Is there a way of getting the index of the item in the collection?
      You should have mentioned the type of collection. Is it
      ToolStripItemCo llection? Why not look at the members available? I see
      the IndexOf method. It probably returns the index of the item.


      Armin

      Comment

      • Academia

        #4
        Re: Is there a way of getting the index of an item in a collection of ToolStripMenuIt ems?


        This is why the method is called:

        For Each mI As ToolStripItem In Me.MenuItemGene ral_Tool.DropDo wnItems

        AddHandler mI.Click, AddressOf HandlesTool_Cli ck

        Next mI



        and this is how index is used

        CType(MenuItemG eneral_Tool.Dro pDownItems(menu Tag),
        ToolStripMenuIt em).Checked = True

        ===

        the ToolStripMenuIt em.CheckOnClick doesn't work for me because I don't want
        it unchecked automatically.

        thanks


        "Academia" <academiaNOSPAM @a-znet.comwrote in message
        news:%23ySaqDFe IHA.5208@TK2MSF TNGP04.phx.gbl. ..
        >I use to do this:
        >
        Dim index As Integer = CType(sender, MenuItem).Index
        >
        but ToolStripMenuIt ems do not have an Index property so I can set the Tag
        or MergeIndex property of those items and use that.
        >
        But that is error prone since changes in the menu require that those
        values be kept synchronized.
        >
        Even though there is no Index property I have the feeling that the
        collection might have the equivalent.
        >
        >
        >
        Is there a way of getting the index of the item in the collection?
        >
        >
        >
        >
        >
        Thanks
        >
        >

        Comment

        • Steve Gerrard

          #5
          Re: Is there a way of getting the index of an item in a collection of ToolStripMenuIt ems?

          Academia wrote:
          >
          I use to do this:
          Dim index As Integer = CType(sender, MenuItem).Index
          and this is how index is used
          >
          CType(MenuItemG eneral_Tool.Dro pDownItems(menu Tag),
          ToolStripMenuIt em).Checked = True
          >
          You are walking around the block to get next door. There is no need to take the
          sender, find its index (or tag), then use that to retrieve the item from the
          parent collection. You already have the item; just use it directly.

          CType(sender, TooStripMenuIte m).Checked = True


          Comment

          • Academia

            #6
            Re: Is there a way of getting the index of an item in a collection of ToolStripMenuIt ems?

            I ask sometimes because I want to be sure I'm not missing something simple.

            Good thing I don't try to play chess.

            thanks

            "Steve Gerrard" <mynamehere@com cast.netwrote in message
            news:BPqdnTVOpc n1pFnanZ2dnUVZ_ jKdnZ2d@comcast .com...
            Academia wrote:
            >>
            >I use to do this:
            >Dim index As Integer = CType(sender, MenuItem).Index
            >
            >and this is how index is used
            >>
            >CType(MenuItem General_Tool.Dr opDownItems(men uTag),
            >ToolStripMenuI tem).Checked = True
            >>
            >
            You are walking around the block to get next door. There is no need to
            take the sender, find its index (or tag), then use that to retrieve the
            item from the parent collection. You already have the item; just use it
            directly.
            >
            CType(sender, TooStripMenuIte m).Checked = True
            >
            >

            Comment

            Working...