Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

problem removing custom menuitem from chart

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    problem removing custom menuitem from chart

    Hello,

    I have the following code in my indicator to add a context menu item to the right click chart. It works perfectly, I it seems to somtimes fail to be removed, in that (not every time), if the chart is refresh with f5, or a change to indicator made, the pjsHABars menu item is still there and becomes duplicated.

    What am I going wrong?

    Thanks,

    Code:
                }  else if (State == State.Configure)
                {
                    //HMA, SSF2, SSF3, WMA, LREG, DHMA, KAMA, ZLEMA, T3, VMA 
                    myContextItem = new MenuItem() { Header = "pjsHABars" };
                    myContextItem.Items.Add( myContextItemtype = new MenuItem() { Header = "Smoothing type" });
                    myContextItem.Items.Add( myContextItemperiod = new MenuItem() { Header = "Smoothing Period" });
                    myContextItemtype.Items.Add( myContextItemtypeHMA = new MenuItem() { Header = "HMA" });
                    myContextItemtype.Items.Add( myContextItemtypeVMA = new MenuItem() { Header = "VMA" });
                    myContextItemtype.Items.Add( myContextItemtypeSSF = new MenuItem() { Header = "SSF" });
                    myContextItemtypeHMA.Click += myContextItemEMA_Click;
                    myContextItemtypeSSF.Click += myContextItemSSF_Click;
                    myContextItemtypeVMA.Click += myContextItemVMA_Click;
                    myContextItemperiod.Items.Add( myContextItemperiod1 = new MenuItem() { Header = "1" });
                    myContextItemperiod.Items.Add( myContextItemperiod2 = new MenuItem() { Header = "2" });
                    myContextItemperiod.Items.Add( myContextItemperiod3 = new MenuItem() { Header = "3" });
                    myContextItemperiod.Items.Add( myContextItemperiod4 = new MenuItem() { Header = "4" });
                    myContextItemperiod.Items.Add( myContextItemperiod5 = new MenuItem() { Header = "5" });
                    myContextItemperiod1.Click += myContextItemperiod1_Click;
                    myContextItemperiod2.Click += myContextItemperiod2_Click;
                    myContextItemperiod3.Click += myContextItemperiod3_Click;
                    myContextItemperiod4.Click += myContextItemperiod4_Click;
                    myContextItemperiod5.Click += myContextItemperiod5_Click;
                }
                else if (State == State.Terminated)
                {
                    if (myContextItem != null)
                    {
                        if (ChartControl != null)
                        {
                            myContextItemperiod1.Click -= myContextItemperiod1_Click;
                            myContextItemperiod2.Click -= myContextItemperiod2_Click;
                            myContextItemperiod3.Click -= myContextItemperiod3_Click;
                            myContextItemperiod4.Click -= myContextItemperiod4_Click;
                            myContextItemperiod5.Click -= myContextItemperiod5_Click;        
                            myContextItemtypeHMA.Click -= myContextItemEMA_Click;
                            myContextItemtypeSSF.Click -= myContextItemSSF_Click;
                            myContextItemtypeVMA.Click -= myContextItemVMA_Click;
    //                        ChartControl.ContextMenu.Items.Remove(myContextItemperiod1);
    //                        ChartControl.ContextMenu.Items.Remove(myContextItemperiod2);
    //                        ChartControl.ContextMenu.Items.Remove(myContextItemperiod3);
    //                        ChartControl.ContextMenu.Items.Remove(myContextItemperiod4);
    //                        ChartControl.ContextMenu.Items.Remove(myContextItemperiod5);
    //                        ChartControl.ContextMenu.Items.Remove(myContextItemtypeVMA);
    //                        ChartControl.ContextMenu.Items.Remove(myContextItemtypeHMA);
    //                        ChartControl.ContextMenu.Items.Remove(myContextItemtypeSSF);
    //                        ChartControl.ContextMenu.Items.Remove(myContextItemtype);
    //                        ChartControl.ContextMenu.Items.Remove(myContextItemperiod);
    
                            ChartControl.ContextMenu.Items.Remove(myContextItem);

    #2
    Hello pjsmith,

    This may be if you are modifying the Items collection directly, it is generally suggested to instead use the controls ContextMenuOpening/.ContextMenuClosing events to dynamically add/remove content from a context menu. I had previously covered this in the following post:


    This sample adds items when a specific indicator is selected, however this could be used to see the events for opening and closing the menu. I would suggest trying this approach to see if you still have the same result.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Aviram Y, Today, 05:29 AM
    0 responses
    3 views
    0 likes
    Last Post Aviram Y  
    Started by quantismo, 04-17-2024, 05:13 PM
    3 responses
    27 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by ScottWalsh, 04-16-2024, 04:29 PM
    7 responses
    35 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by cls71, Today, 04:45 AM
    0 responses
    6 views
    0 likes
    Last Post cls71
    by cls71
     
    Started by mjairg, 07-20-2023, 11:57 PM
    3 responses
    218 views
    1 like
    Last Post PaulMohn  
    Working...
    X