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

Buttons & Tabs

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

    Buttons & Tabs

    HI
    In the example code you have
    myButton.Visibility = Visibility.Collapsed;

    which says a button will not show if the tab is not active.

    Can you define active as when I have 3 tabs with my button indicator on all 3 charts I get 3 buttons.
    I tried
    myButton.Visibility = Visibility.Hidden;
    but that didn't make any difference.

    Am I misunderstanding the word 'active' here or have I made some other error?

    Here is all my code to do with this
    Code:
              myButton.Visibility = Visibility.Collapsed;
              foreach (TabItem tab in this.chartWindow.MainTabControl.Items)
    Edit _ I should add it has a bitmap image if that makes any differene?
    Last edited by Mindset; 03-18-2024, 11:17 PM.

    #2
    Hello Mindset,

    Thank you for your post.

    Which example script are you referring to?

    Visibility.Hidden hides the control, but reserves the space it occupies in the layout. So it renders whitespace instead of the control. Visibilty.Collapsed does not render the control and does not reserve the whitespace.​



    An active tab is a tab that is currently being used/displayed (not minimized or otherwise being displayed).

    Please let me know if I can assist further.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Hello Gaby

      Pic is worth a thousand words.

      Click image for larger version

Name:	Screenshot 2024-03-20 110229.png
Views:	45
Size:	52.7 KB
ID:	1296463


      So I have the indicator with the button in the top toolbar. All works fine but as I have only 1 active chart ( to my knowledge) should I not only see 1 button?

      The script I use on On State Change is below

      Code:
                  else if (State == State.Historical)
                  {
                        if (!IsToolBarButtonAdded)
                        {
                               if(ChartControl != null)
                               {
                          ChartControl.Dispatcher.InvokeAsync((Action)(() =>
                          {
                          
                      
                myBitmapImage.BeginInit();
                myBitmapImage.UriSource = new Uri(NinjaTrader.Core.Globals.InstallDir + "switchcolour.png");
              myBitmapImage.EndInit();
       
                // Assign the BitmapImage as the ImageSource of the ImageBrush
                backgroundImage.ImageSource = myBitmapImage;    
                              //myBitmapImage.
                  }));                
                        AddButtonToToolbar();    
                        }
                      }        
                  }​
      and here is my cleanup invoked in State.Terminated
      Code:
            private void DisposeCleanUp()
        {
            //ChartWindow Null Check
            if (chartWindow != null)
            {
                //Dispatcher used to Assure Executed on UI Thread
                ChartControl.Dispatcher.InvokeAsync((Action)(() =>
                {
                    //Button Null Check
                    if (myButton != null)
                    {
                        //Remove Button from Indicator's Chart ToolBar
                        chartWindow.MainMenu.Remove(myButton);
                        myButton.Click -= OnMouseClickRHS;
                        ChartControl.ChartPanels[0].MouseDown -= MiddleButton;
                    }
                }));
            }
        }​
      Attached Files

      Comment


        #4
        Hello Mindset,

        To clarify, is your script meant to be creating 3 buttons? Or are you only creating 1 button?

        Where in the code have you changed the visibility of the button(s)?

        If you have added 3 buttons to the chart from the script, if the visibility of these buttons is only set to be Collapsed or Hidden when the chart is inactive, all 3 buttons will show on the chart when it is active. Even if you only have 1 active chart open, if the script is adding 3 buttons to the chart it is being applied to, 3 buttons will be visible on that chart.

        I look forward to your response.
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Gaby
          I have 3 charts - each with the same script which produces one button.
          You seem to be saying I should see all 3 buttons despite only 1 chart being active - is that right?

          Here is the code to make the buttons collapse/visible

          Code:
                 
              private void AddButtonToToolbar()
            {
               
                ChartControl.Dispatcher.InvokeAsync((Action)(() =>
                {
                   
                    chartWindow = Window.GetWindow(this.ChartControl.Parent) as Chart;
                    if (chartWindow == null)
                    {
                      
                        return;
                    }
           
                    // Create a style to apply to the button
                    Style s = new Style();
                    s.TargetType = typeof(System.Windows.Controls.Button);
          
                    myButton = new System.Windows.Controls.Button();
           
                
                        myButton.Style = s;//basicButtonStyle;
                        myButton.BorderThickness = new Thickness(1,1,1,1);//Starts off as Indicator OFF - needs to be switched on
                      myButton.BorderBrush= Brushes.White;
               
                    myButton.Background = backgroundImage;
                     myButton.Width = 30;
                    myButton.Margin = new Thickness(5, 5, 0, 5);
           
                    myButton.Content = "";
                    myButton.IsEnabled = true;
                    myButton.HorizontalAlignment = HorizontalAlignment.Left;
                     myButton.Click += OnMouseClickRHS;
                    myButton.ToolTip = "Alt+Click = on/off\nCtrl Click to Extend lines\nNormal Click to swap sides";
                  
                    ChartControl.ChartPanels[0].MouseDown += MiddleButton;
          
                    // Add the Button to the Chart's Toolbar
                    chartWindow.MainMenu.Add(myButton);
           
                    //Prevent the Button From Displaying when WorkSpace Opens if it is not in an active tab
                    myButton.Visibility = Visibility.Collapsed;
                    foreach (TabItem tab in this.chartWindow.MainTabControl.Items)
                    {
                        if ((tab.Content as ChartTab).ChartControl == this.ChartControl
          
                             && tab == this.chartWindow.MainTabControl.SelectedItem)
                        {
                            myButton.Visibility = Visibility.Visible;
                        }
                    }
                    IsToolBarButtonAdded = true;
                }));
            }​
          Last edited by Mindset; 03-20-2024, 07:26 AM.

          Comment


            #6
            Hello Mindset,

            Thank you for your response.

            It looks like you are trying to do the hiding/visible actions in your AddButtonToToolbar method, this wouldn't work since the method is only run when it is applied and not when the tab changes.

            The ChartToolBarCustomMenuSample script from the post below has code that demonstrates finding which tab is active and also controlling adding/removing items. You can use the tab selection changed event to hide/show items.

            Hello All, Moving forward this will be maintained in the help guide reference samples and no longer maintained on the forum. Creating Chart WPF (UI) Modifications from an Indicator - https://ninjatrader.com/support/help...ui)-modifi.htm (https://ninjatrader.com/support/helpGuides/nt8/creating-chart-wpf-(ui)-modifi.htm) I've


            Please let me know if I can assist further.
            Gaby V.NinjaTrader Customer Service

            Comment


              #7
              Hi Gaby
              That makes perfect sense!
              I have tried the following however without success

              Code:
                                          #region TAB SELECTION
                      private bool TabSelected()
                      {
                          bool tabSelected = false;
              
                          // loop through each tab and see if the tab this indicator is added to is the selected item
                          // full qualified namespaces used here to show where these tools are
                          foreach (TabItem tab in chartWindow.MainTabControl.Items)
                              if ((tab.Content as ChartTab).ChartControl == ChartControl && tab == chartWindow.MainTabControl.SelectedItem)
                                  tabSelected = true;
              //          foreach (TabItem tab in this.chartWindow.MainTabControl.Items)
              //          {
              //              if ((tab.Content as ChartTab).ChartControl == this.ChartControl
              
              //                   && tab == this.chartWindow.MainTabControl.SelectedItem)
              //              {
              //                  myButton.Visibility = Visibility.Visible;
              //              }
              //          }
                          return tabSelected;
                      }
              
                      // Runs ShowWPFControls if this is the selected chart tab, other wise runs HideWPFControls()
                      private void TabChangedHandler(object sender, SelectionChangedEventArgs e)
                      {
                          if (e.AddedItems.Count <= 0)
                              return;
              
                          tabItem = e.AddedItems[0] as TabItem;
                          if (tabItem == null)
                              return;
              
                          chartTab = tabItem.Content as ChartTab;
                          if (chartTab == null)
                              return;
              
                          if (TabSelected())
                              //try {
                                  myButton.Visibility = Visibility.Visible;
                                  //}
                              //catch (System.Exception ef) { Print(ef.ToString()); }
                          else
                              myButton.Visibility = Visibility.Collapsed;
                          
                                // myButton.Visibility = Visibility.Collapsed;
              //          foreach (TabItem tab in this.chartWindow.MainTabControl.Items)
              //          {
              //              if ((tab.Content as ChartTab).ChartControl == this.ChartControl
              
              //                   && tab == this.chartWindow.MainTabControl.SelectedItem)
              //              {
              //                  myButton.Visibility = Visibility.Visible;
              //              }
              //          }
                          
                      }
              #endregion
              ​
              Last edited by Mindset; 03-20-2024, 09:35 PM. Reason: added all the code

              Comment


                #8
                Hello Mindset,

                Thank you for your inquiry.

                I recommend taking a look at ChartToolBarCustomMenuSample instead of ChartCustomToolBarExample (the names are very similiar). The code you are trying is from ChartCustomToolBarExample.


                ChartToolBarCustomMenuSample demonstrates hiding the actual visibility of the menu item. You can do something similar for your button.

                Below is just a snippet of the code that handles the visibility but I recommend taking a look at the entire script for more clarity on how this method is used.

                Code:
                private void MySelectionChangedHandler(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
                {
                if (e.AddedItems.Count <= 0)
                return;
                
                tabItem = e.AddedItems[0] as System.Windows.Controls.TabItem;
                if (tabItem == null)
                return;
                
                chartTab = tabItem.Content as NinjaTrader.Gui.Chart.ChartTab;
                if (chartTab != null)
                if (topMenuItem != null)
                topMenuItem.Visibility = chartTab.ChartControl == ChartControl ? Visibility.Visible : Visibility.Collapsed;
                }
                Please let me know if I can assist further. ​
                Gaby V.NinjaTrader Customer Service

                Comment


                  #9
                  In my experience, some trading platforms let you sell 2 shares in one go to switch from having 1 share to shorting 1 share. I'm curious if that's doable here.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by fx.practic, 10-15-2013, 12:53 AM
                  5 responses
                  5,403 views
                  0 likes
                  Last Post Bidder
                  by Bidder
                   
                  Started by Shai Samuel, 07-02-2022, 02:46 PM
                  4 responses
                  94 views
                  0 likes
                  Last Post Bidder
                  by Bidder
                   
                  Started by DJ888, Yesterday, 10:57 PM
                  0 responses
                  6 views
                  0 likes
                  Last Post DJ888
                  by DJ888
                   
                  Started by MacDad, 02-25-2024, 11:48 PM
                  7 responses
                  158 views
                  0 likes
                  Last Post loganjarosz123  
                  Started by Belfortbucks, Yesterday, 09:29 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post Belfortbucks  
                  Working...
                  X