Dynamic menu items and event handlers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?ZWFndWlsYXI=?=

    Dynamic menu items and event handlers

    Hi,

    I am trying to dynamically generate a menu, based on entries on a text or
    xml file. The text file contains the "tree" after which the menu will need to
    be created. Something like the following:

    Level 1
    -- Level 2
    -- Level 2
    Level 1
    -- Level 2
    ---- Level 3

    I can read the file and generate each menu item definition, with the
    corresponding "nesting", without a problem.

    What I'm having difficulties with is deciding on how to handle the events
    for each of the menu items. Generating the full menu dynamically would mean
    having to create the event handlers dynamically, unless I have a fixed amount
    of event handlers, which I'm not sure is the best approach. I might be wrong
    here...

    1. What's the best way to generate event handlers dynamically? I've read
    about runtime IL generation, which makes sense but is a bit complex.

    2. Using regular event handlers (hard coded), can I capture which of the
    menu items is being clicked and what component is the menu being displayed
    on? i.e., if the menu is being displayed after a right click on a button or a
    picture box, can I: 1) know if it's the picture box or the button displaying
    the menu? and 2) can I know what menu/sub menu is being clicked?

    Any ideas will be greatly appreciated.

    Thanks in advance,

    </edwin>
  • Herfried K. Wagner [MVP]

    #2
    Re: Dynamic menu items and event handlers

    "eaguilar" <eaguilar@discu ssions.microsof t.comschrieb:
    I am trying to dynamically generate a menu, based on entries on a text or
    xml file. The text file contains the "tree" after which the menu will need
    to
    be created. Something like the following:
    >
    Level 1
    -- Level 2
    -- Level 2
    Level 1
    -- Level 2
    ---- Level 3
    >
    I can read the file and generate each menu item definition, with the
    corresponding "nesting", without a problem.
    >
    What I'm having difficulties with is deciding on how to handle the events
    for each of the menu items. Generating the full menu dynamically would
    mean
    having to create the event handlers dynamically, unless I have a fixed
    amount
    of event handlers, which I'm not sure is the best approach. I might be
    wrong
    here...
    >
    1. What's the best way to generate event handlers dynamically? I've read
    about runtime IL generation, which makes sense but is a bit complex.
    Take a look at the 'AddHandler' and 'RemoveHandler' statements.
    2. Using regular event handlers (hard coded), can I capture which of the
    menu items is being clicked
    Check out the event handler's 'sender' parameter. It contains a reference
    to the source of the event.
    and what component is the menu being displayed
    on?
    Context menu objects have a 'SourceControl' property which references the
    control the menu has been shown on.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

    Comment

    • Jack Jackson

      #3
      Re: Dynamic menu items and event handlers

      On Wed, 20 Feb 2008 07:28:01 -0800, eaguilar
      <eaguilar@discu ssions.microsof t.comwrote:
      >Hi,
      >
      >I am trying to dynamically generate a menu, based on entries on a text or
      >xml file. The text file contains the "tree" after which the menu will need to
      >be created. Something like the following:
      >
      >Level 1
      -- Level 2
      -- Level 2
      >Level 1
      -- Level 2
      ---- Level 3
      >
      >I can read the file and generate each menu item definition, with the
      >correspondin g "nesting", without a problem.
      >
      >What I'm having difficulties with is deciding on how to handle the events
      >for each of the menu items. Generating the full menu dynamically would mean
      >having to create the event handlers dynamically, unless I have a fixed amount
      >of event handlers, which I'm not sure is the best approach. I might be wrong
      >here...
      >
      >1. What's the best way to generate event handlers dynamically? I've read
      >about runtime IL generation, which makes sense but is a bit complex.
      >
      >2. Using regular event handlers (hard coded), can I capture which of the
      >menu items is being clicked and what component is the menu being displayed
      >on? i.e., if the menu is being displayed after a right click on a button or a
      >picture box, can I: 1) know if it's the picture box or the button displaying
      >the menu? and 2) can I know what menu/sub menu is being clicked?
      >
      >Any ideas will be greatly appreciated.
      >
      >Thanks in advance,
      >
      ></edwin>
      I would use AddHandler (in VB) to add a single handler to the
      ItemClicked event on all of the menus. The event handler has two
      arguments. 'sender' is the object (menu) that raised the event. 'e'
      is the event information, and e.ClickedItem will be the item in the
      'sender' menu that was clicked.

      You can use various properties of the menu items (Tag, Name, Text) to
      identify which item it is.

      If the same set of menus are connected to multiple controls, then you
      might need to handle the Opening event of the top menu and capture its
      owning control at that time.

      Comment

      • =?Utf-8?B?ZWFndWlsYXI=?=

        #4
        Re: Dynamic menu items and event handlers

        Hi Herfried, thanks for the reply.

        I am already using AddHandler to create que events and their handlers, and
        the sender property to access some info. Both are working fine.

        SourceControl property is partially working - for "Level 1" entry objects I
        can access and display the name of the control triggering the menu, but for
        "higher" level entries, SourceControl returns Nothing for some reason...

        Any ideas? I could send code samples if needed...

        Thanks in advance again,
        --
        </edwin>

        Comment

        Working...