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

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 senderKeyEventArgs 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 != nullqs.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.


      JesseNinjaTrader Customer Service

      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(1GridUnitType.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 0chartGrid.Children.Counti++)
              {
                if (
        System.Windows.Controls.Grid.GetColumn(chartGridChildren[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(topMenuSystem.Windows.Controls.Grid.GetColumn(chartWindow .MainTabControl));
              
        System.Windows.Controls.Grid.SetRow(topMenutabControlStartRow);

              
        System.Windows.Controls.Grid.SetColumn(leftInnerGr idtabControlStartColumn);
              
        System.Windows.Controls.Grid.SetRow(leftInnerGridSystem.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;





          JesseNinjaTrader Customer Service

          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.

              JesseNinjaTrader Customer Service

              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 senderKeyEventArgs 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 != nullqs.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 LiamTwine, Today, 08:10 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post LiamTwine  
                  Started by Balage0922, Today, 07:38 AM
                  0 responses
                  5 views
                  0 likes
                  Last Post Balage0922  
                  Started by JoMoon2024, Today, 06:56 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post JoMoon2024  
                  Started by Haiasi, 04-25-2024, 06:53 PM
                  2 responses
                  19 views
                  0 likes
                  Last Post Massinisa  
                  Started by Creamers, Today, 05:32 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post Creamers  
                  Working...
                  X