Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Set QuantityUpDown value from keyboard

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

    #31
    Hello Jesse, how could I set the default QuantitySelector value to 1? Currently it is 0 when loading the indicator on the chart and ?'d like to to set it to one if possible as default. Thanks!

    I tried setting it to 1 just above the method but it's not working.

    PHP Code:
        NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector = null;
    
        // QS
        private bool Is_ToolBar_Controls_Added;
    
        private int qs;
    
    protected override void OnStateChange()
    { 
    

    PHP Code:
       NinjaTrader.Gui.Tools.QuantityUpDown qs = sender as NinjaTrader.Gui.Tools.QuantityUpDown;
        qs.Value = 1;
    
        private void On_quantitySelector_PreviewKeyDown(object sender, KeyEventArgs p)
        {
          NinjaTrader.Gui.Tools.QuantityUpDown qs = sender as NinjaTrader.Gui.Tools.QuantityUpDown;
    
          if (p.Key == Key.Delete || p.Key == Key.Back)
          {
            p.Handled = true;
            qs.Value = 0;
          }
    
          if (p.Key >= Key.D0 && p.Key <= Key.D9)
          {
            p.Handled = true;
    
            string number = p.Key.ToString();
            string newnumber = qs.Value.ToString();
    
            number = number.Replace("NumPad", "");
            number = number.Replace("D", "");
            int num = int.Parse(newnumber + number);
    
            if (qs != null) qs.Value = num;
          }
        } 
    

    Comment


      #32
      Hello PaulMohn,

      When you create the instance of the selector you would need to set the value at that point.

      Generally UI controls can be created in State.DataLoaded and beyond. In most of the UI samples from the forum all of the UI work is done in a method called InsertWPFControls, that would be a good point to set a default.

      This code would be too late to set a default:

      NinjaTrader.Gui.Tools.QuantityUpDown qs = sender as NinjaTrader.Gui.Tools.QuantityUpDown;
      qs.Value = 1;

      The sender is from an event so that would be after the control was created.


      Comment


        #33
        Ok thanks I've moved it to the State.DataLoaded scope and removed NinjaTrader.Gui.Tools.QuantityUpDown qs = sender as NinjaTrader.Gui.Tools.QuantityUpDown;
        PHP Code:
            NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector = null;
        
            // QS
            private bool Is_ToolBar_Controls_Added;
        
            private int qs;
        
        protected override void OnStateChange()
        { 
        

        PHP Code:
        else if (State == State.DataLoaded)
        {
           qs.Value = 1; 
        

        now I get the error
        NinjaScript File Error Code Line Column
        Testa.cs 'int' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?) CS1061 624 8
        How do I get it working? Thanks!


        i've checked Chelsea's InsertWPFControls() below

        ChartCustomToolbarExample_NT8.zip (3.9 KB, 1073 views)

        PHP Code:
        
            protected void InsertWPFControls()
            {
              if (panelActive)
                return;
        
              if (chartGrid.RowDefinitions.Count == 0)
                chartGrid.RowDefinitions.Add(new System.Windows.Controls.RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
        
              tabControlStartColumn   = System.Windows.Controls.Grid.GetColumn(chartWindow .MainTabControl);
              tabControlStartRow     = System.Windows.Controls.Grid.GetRow(chartWindow.Ma inTabControl);
        
              chartGrid.ColumnDefinitions.Insert(tabControlStart Column, new System.Windows.Controls.ColumnDefinition() { Width = new GridLength(30) });
              chartGrid.RowDefinitions.Insert(tabControlStartRow , new System.Windows.Controls.RowDefinition() { Height = new GridLength(20) });
        
              // including the chartTabControl move all items right of the chart and below the chart to the right one column and down one row
              for (int i = 0; i < chartGrid.Children.Count; i++)
              {
                if (System.Windows.Controls.Grid.GetColumn(chartGrid. Children[i]) >= tabControlStartColumn)
                  System.Windows.Controls.Grid.SetColumn(chartGrid.C hildren[i], System.Windows.Controls.Grid.GetColumn(chartGrid.C hildren[i]) + 1);
        
                if (System.Windows.Controls.Grid.GetRow(chartGrid.Chi ldren[i]) >= tabControlStartRow)
                  System.Windows.Controls.Grid.SetRow(chartGrid.Chil dren[i], System.Windows.Controls.Grid.GetRow(chartGrid.Chil dren[i]) + 1);
              }
        
              // set the columns and rows for our new items
              System.Windows.Controls.Grid.SetColumn(topMenu, System.Windows.Controls.Grid.GetColumn(chartWindow .MainTabControl));
              System.Windows.Controls.Grid.SetRow(topMenu, tabControlStartRow);
        
              System.Windows.Controls.Grid.SetColumn(leftInnerGr id, tabControlStartColumn);
              System.Windows.Controls.Grid.SetRow(leftInnerGrid, System.Windows.Controls.Grid.GetRow(chartWindow.Ma inTabControl));
        
              chartGrid.Children.Add(topMenu);
              chartGrid.Children.Add(leftInnerGrid);
        
              // let the script know the panel is active
              panelActive = true;
            } 
        

        But I'm not sure how to make use of it since it doesn't uses QuantitySelector variable in the InsertWPFControls().
        Last edited by PaulMohn; 03-17-2022, 11:00 AM.

        Comment


          #34
          Hello

          You made a new variable named qs but its an int, that is not what you need to do. You need to remove that variable to avoid that error. The qs variable needs to be your quantity selectors variable. If you added the quantity selector then you should already have a variable for that in your script. That is going to be where you add the control to the chart. You should be able to find that by looking for where you initially subscribe to the preview key method: += On_quantitySelector_PreviewKeyDown;





          Comment


            #35
            Got it many thanks!

            I removed

            qs.Value = 1;

            from the State.DataLoaded
            PHP Code:
            else if (State == State.DataLoaded)
            {
               // qs.Value = 1; REMOVED 
            

            And I added

            quantitySelector.Value = 1;

            At the end of the #region Add Controls To Toolbar

            PHP Code:
                #region Add Controls To Toolbar
                private void Add_Controls_To_Toolbar()
                {
                  // Use this.Dispatcher to ensure code is executed on the proper thread
                  ChartControl.Dispatcher.InvokeAsync((Action)(() =>
                  {
            
                    //Obtain the Chart on which the indicator is configured
                    chartWindow = Window.GetWindow(this.ChartControl.Parent) as Chart;
                    if (chartWindow == null)
                    {
                      Print("chartWindow == null");
                      return;
                    }
            
                    quantitySelector = new NinjaTrader.Gui.Tools.QuantityUpDown();
                    //quantitySelector.ValueChanged += On_quantitySelector_ValueChanged;
                    quantitySelector.PreviewKeyDown += On_quantitySelector_PreviewKeyDown;
            
                    chartWindow.MainMenu.Add(quantitySelector);
            
                    Is_ToolBar_Controls_Added = true;
            
                    quantitySelector.Value = 1;
                  }));
                }
                #endregion 
            

            So that it sets the value at the time it adds the Control? Thanks?

            Comment


              #36
              Hello PaulMohn,

              Correct, after the control is created you can modify any of its properties and then add it to the chart.

              Comment


                #37
                Originally posted by PaulMohn View Post
                Hello Jesse, how could I set the default QuantitySelector value to 1? Currently it is 0 when loading the indicator on the chart and ?'d like to to set it to one if possible as default. Thanks!

                I tried setting it to 1 just above the method but it's not working.

                PHP Code:
                NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector = null;
                
                // QS
                private bool Is_ToolBar_Controls_Added;
                
                private int qs;
                
                protected override void OnStateChange()
                { 
                

                PHP Code:
                NinjaTrader.Gui.Tools.QuantityUpDown qs = sender as NinjaTrader.Gui.Tools.QuantityUpDown;
                qs.Value = 1;
                
                private void On_quantitySelector_PreviewKeyDown(object sender, KeyEventArgs p)
                {
                NinjaTrader.Gui.Tools.QuantityUpDown qs = sender as NinjaTrader.Gui.Tools.QuantityUpDown;
                
                if (p.Key == Key.Delete || p.Key == Key.Back)
                {
                p.Handled = true;
                qs.Value = 0;
                }
                
                if (p.Key >= Key.D0 && p.Key <= Key.D9)
                {
                p.Handled = true;
                
                string number = p.Key.ToString();
                string newnumber = qs.Value.ToString();
                
                number = number.Replace("NumPad", "");
                number = number.Replace("D", "");
                int num = int.Parse(newnumber + number);
                
                if (qs != null) qs.Value = num;
                }
                } 
                
                Thanks #PaulMohn and NT - this was really useful.

                Comment


                  #38
                  if I want to change the colour of the background

                  if(quantitySelector.Value > 1)
                  quantitySelector.Background. = Brushes.Red;​

                  doesn't work?
                  What's the correct approach for this?

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  637 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  366 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  107 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  569 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  571 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X