Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

How to dynamically add event handlers to D365 menu items

Posted on by 20
Hi, I'm new to D365 fin ops. I've managed to create a new form based on the Simple List and I'm currently trying to customise the Action Pane.
 
I am dynamically creating the menu items based on the contents of some tables, namely ClimateControlType and ClimateControlOption.
 
My problem now is how to handle the OnClicked event of the menu items in code.
 
This is what I have but I'm not sure how to progress from here:
 
    public void init()
    {
        super();
        FormButtonGroupControl btnGroup = FormActionPaneControl.addControl(FormControlType::ButtonGroup, 'ButtonGroup');
        FormMenuButtonControl btnSetAll = btnGroup.addControl(FormControlType::MenuButton, 'btnSetAll');
        btnSetAll.text('Set all');
        ClimateControlOption climateOptions;
        while select climateOptions
        {
            FormButtonControl btn = btnSetAll.addControl(FormControlType::Button, 'btnSetAll'+climateOptions.ClimateControlOptionCode);
            btn.text(climateOptions.ClimateControlOptionDescription);
            btn.OnClicked = SetAllClick;
        }
        ClimateControlType climateTypes;
        while select climateTypes
        {
            FormMenuButtonControl btnSetAllType = btnGroup.addControl(FormControlType::MenuButton, 'btnSetAllType'+climateTypes.ClimateControlTypeCode);
            btnSetAllType.text('Set all '+climateTypes.ClimateControlTypeDescription);
            while select climateOptions
            {
                FormButtonControl btn = btnSetAllType.addControl(FormControlType::Button, 'btnSetAllType'+climateTypes.ClimateControlTypeCode+climateOptions.ClimateControlOptionCode);
                btn.text(climateOptions.ClimateControlOptionDescription);
            }
        }
    }
    public void SetAllClick(FormControl sender, FormControlEventArgs e)
    {
    }
  • Community member Profile Picture
    Community member 20 on at
    How to dynamically add event handlers to D365 menu items
    I'm trying to mark your replay as the answer Martin but the forum just seems to hang with the busy indicator when I do it.
  • Community member Profile Picture
    Community member 20 on at
    How to dynamically add event handlers to D365 menu items
    I think I've figured it out.  I was using the double colon rather than a period.
  • Community member Profile Picture
    Community member 20 on at
    How to dynamically add event handlers to D365 menu items
    Thanks Martin,  is there anyway to specify the current instance like this::myMethod?
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    How to dynamically add event handlers to D365 menu items
    That's a wrong syntax indeed. The correct syntax is
     
    btn.OnClicked += eventHandler(...)
     
    where the parameter may be either instance method (myInstance.myMethod) or a static method (MyType::myMethod).
  • Community member Profile Picture
    Community member 20 on at
    How to dynamically add event handlers to D365 menu items
    This line won't compile and it doesn't seem to recognise the SetAllClick method declared below.
     
    btn.OnClicked = SetAllClick;
     
    I want all the generated menu items to call this method because the code will be very similar.
  • Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    How to dynamically add event handlers to D365 menu items
    What exactly are you struggling with regarding "how to handle the OnClicked event"?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans