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

Referencing MainTabControl.Items.Count

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

    Referencing MainTabControl.Items.Count

    I am trying to reference the last tab in MainTabControl.Items.

    To reference last in a List, I would usually use .Count-1 so MainTabControl.Items[Count-1]. However whenever I try to reference any item in MainTabControl.Items[] it does not work. e.g

    MainTabControl.Items[0]
    or
    int tabCount = MainTabControl.Items.Count;
    MainTabControl.Items[tabCount-1]

    None of the above work.

    Ideally what I want to do is check that MainTabControl.SelectedIndex = MainTabControl.Items[Count-1] (last tab on the window) before I then apply an action such as Focus(). Please can you tell me how I can access the reference for the last tab in the window.

    Thanks

    #2
    Hello b16_aln,

    The Items collection should be what you need here, that is what is used in the other examples from this post: https://ninjatrader.com/support/foru...ions#post96376

    Code:
    foreach (System.Windows.Controls.TabItem tab in [B]chartWindow.MainTabControl.Items[/B])
                    if ((tab.Content as Gui.Chart.ChartTab).ChartControl == ChartControl && tab == chartWindow.MainTabControl.SelectedItem)
                        tabSelected = true;
    What is the specific problem you see when you use MainTabControl.Items[tabCount-1], are you getting a null return or what is happening? If you have a sample you can post or would like to try this in one of the samples in the linked forum post and provide it that would be helpful.


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

    Comment


      #3
      Hi Jesse, thanks for getting back to me. I've been able to work it out using the Count-1 method to set a ChartTab chartTab as the last tab in the window, i'm not entirely sure what I did wrong, I think it was related to improper casting from tabindex to tab item.

      I'm now having a problem that I cannot focus on chartTab. What I want to do is leave the chart window running in the background with the indicator on it, when a certain IF statement is triggered I want to focus on chartTab and then do something on that chart.

      I'm trying chartTab.Focus() within a dispatcher but it is not working. How can I get the indicator to focus on chartTab?

      Many thanks

      Comment


        #4
        Hello b16_aln,

        I am not clear on what you mean by focus, are you having problems changing the tab or are you asking how to bring the window to the front? The tab its self very likely wont have focus, it would be the window or a specific control like a button.

        Code:
        How can I get the indicator to focus on chartTab?
        Are you asking how to display an indicator on a different chart tab? Are you asking how to get the currently selected tab content?

        I believe in this case if you could walk me through your idea in simple steps that would be helpful toward as solution.



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

        Comment


          #5
          Thanks Jesse,

          To explain;

          I have several windows open, each with several tabs open. All tabs are running the indicator.

          The indicator on each tab is looking for a set of IF statements to occur. When the IF statements occur I want to identify the tab that it occured on, and select that chart so that the indicator can then SendKeys to that chart. Before I can SendKeys, I need to ensure that the correct chart is selected (I assumed this was referred to as being in Focus).

          I thought I could use chartTab.Focus() to select the ensure the correct chart is the active chart and then when I SendKeys it will send it to the correct chart, however this does not appear to be correct. Possibly my terminology is wrong as essentially I want the chart that triggered the IF statement to be brought to the front, made active so that when the indicator does a SendKey command it is sending it to the correct chart.

          Comment


            #6
            I've worked out how to use .Focus() to get what I needed, thanks for your input.

            Last stage is getting SendKeys to work. When I use SendKeys.SendWait it crashes my NT8. Any suggestions how to use it without crashing the system?

            Comment


              #7
              Hello b16_aln,

              Bringing it to the front will relate to the Window and not the tab, this should be able to be controlled in a standard wpf way. I don't have a window sample on hand but you should be able to call myWindow.Activate(); to achieve that.

              If you are using SendKeys that may be another part of the problem, that was mainly for windows forms and is not necessarily what is suggested for WPF due to how focus works. If you are trying to type or click on something it is generally best to get that specific object and then use the automation framework.

              To send keystrokes you can see how that is accomplished in the rollover indications indicator: https://ninjatraderecosystem.com/use...indications-2/

              You can see an example of clicking a control by its automation id here: https://ninjatrader.com/support/foru...gnoreallerrors

              In both cases a specific control is targeted, the rollover indicator specifically shows how the focus is managed to focus the chart before typing.


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

              Comment


                #8
                Thanks Jesse, yes have the Activate() working now but I am getting some issues with translating the Rollover indicator Keyboard send which is based on a mouse click into a straight forward command following a if=true.

                I've copied and pasted the following part of the Rollover code into my indicator but it's not working.
                __________________________________________________ __________________________________________________ ____________________

                // explicity give the chart focus
                ChartControl.OwnerChart.Focus();

                // send a key event to trigger the instrument overlay selector to appear
                Keyboard.FocusedElement.RaiseEvent(new TextCompositionEventArgs(InputManager.Current.Prim aryKeyboardDevice,
                new TextComposition(InputManager.Current, ChartControl.OwnerChart, "open sesame"))
                { RoutedEvent = TextCompositionManager.PreviewTextInputEvent });

                // send the actual string you want in the instrument overlay selector
                Keyboard.FocusedElement.RaiseEvent(new TextCompositionEventArgs(InputManager.Current.Prim aryKeyboardDevice,
                new TextComposition(InputManager.Current, ChartControl.OwnerChart, nextExpiryString))
                { RoutedEvent = TextCompositionManager.TextInputEvent });

                // optionally send the enter key
                if (!Prompt)
                Keyboard.FocusedElement.RaiseEvent(new KeyEventArgs(Keyboard.PrimaryDevice,
                PresentationSource.FromVisual(ChartControl.OwnerCh art), 0, Key.Enter) { RoutedEvent = Keyboard.PreviewKeyDownEvent } );

                __________________________________________________ __________________________________________________ _________________-___

                I get an error stating Promt is not known so I tried taking away the if(!Prompt) part so it would just always send the enter key but that also doesn;t work. When I run the code the chart just hangs rather than opening and entering the text into the text searhc box. Are there any obvious changes I have to make to the code to make it work without a click event as in the orignal Rollover Indicator? I'm pretty stumped at this stage.

                Thanks

                Comment


                  #9
                  Hello b16_aln,

                  That code specifically is to open the chart instrument picker dialog to change the contract. You likely need the remainder of the rollover indicator for that to work or do anything.

                  The code there likely shouldn't be copied/pasted, you would instead want to learn from what it is doing to recreate that logic for the control you want to target. If you are trying to target the chart controls chart then that is how but the other commands being sent require the other logic that indicator uses to work.

                  I just tried the rollover indicator and don't see any kind of freezing and I can see it working, you may want to discard what you were trying with sendkeys to ensure none of that is still being used and try again or try the indicator directly.

                  Are there any obvious changes I have to make to the code to make it work without a click event as in the orignal Rollover Indicator?
                  The only obvious item would be it may require a dispatcher depending on where it was used, otherwise you would likely need to make sure what you are testing actually works before moving it from the known working location. You can test with the button and make sure what it is you want to target/control works and then try it other locations where it may be needed to confirm it works the same.



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

                  Comment


                    #10
                    Originally posted by NinjaTrader_Jesse View Post
                    Hello b16_aln,

                    That code specifically is to open the chart instrument picker dialog to change the contract. You likely need the remainder of the rollover indicator for that to work or do anything.

                    The code there likely shouldn't be copied/pasted, you would instead want to learn from what it is doing to recreate that logic for the control you want to target. If you are trying to target the chart controls chart then that is how but the other commands being sent require the other logic that indicator uses to work.

                    I just tried the rollover indicator and don't see any kind of freezing and I can see it working, you may want to discard what you were trying with sendkeys to ensure none of that is still being used and try again or try the indicator directly.


                    The only obvious item would be it may require a dispatcher depending on where it was used, otherwise you would likely need to make sure what you are testing actually works before moving it from the known working location. You can test with the button and make sure what it is you want to target/control works and then try it other locations where it may be needed to confirm it works the same.



                    I look forward to being of further assistance.
                    Hi Jesse,

                    Yes, that's what I'm looking to do, open and change the instrument picker however not via a button click or SendKeys. Sendkeys just crashes the platform. Can you suggest the code that I could use to open the instrument picker without using SendKeys please? I have the logic to correctly start after a certain condition, it now correctly selects the correct chart but now I want to access the instrument picker via the code, not via a click button. I don't know how to do this, so could you suggest some code that might work or point me in the right direction, I've tried various adaptations of the rollover instrument picker code but cannot get it to work, I have no experience of using dispatchers and this is where I think i'm going wrong.

                    Thanks

                    Comment


                      #11
                      Hello b16_aln,

                      Sendkeys just crashes the platform.
                      Right, as noted that is for windows forms and generally won't work correctly in wpf. You can safely discard this knowledge as it won't apply to NT8.

                      What you are asking to do is specifically a keyboard action of the chart so if you want to do that you would need to use the code in the rollover indicator.

                      If your test didn't work I would suggest trying again but likely starting with the full code of the rollover indicator to start from a working point. If you don't want to use a button a good starting place would be to start with the rollover indicator and make just that modification. You could move the buttons action to a condition in OnBarUpdate or somewhere else to test it.

                      Because this uses User Interface controls you will need to use a dispatcher from most event driven overrides. A dispatcher simply tells that code to run on the correct thread. You can see a dispatcher from the ChartControl being used on line 72 of the rollover indicator, you wrap your code in the action's body similar to an "if" statement and the code inside the body { } is executed on the ChartControls thread. If you put the buttons code in OnBarUpdate you would also need the code from line 72 before it and you would put the buttons code inside the body {} just like how it wraps the method on like 74.

                      Code:
                      ChartControl.Dispatcher.InvokeAsync((Action)(() =>
                      {
                          //code here
                      }));
                      I look forward to being of further assistance.



                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        Thanks Jesse, I will try that and see if I can get to the bottom of it.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by junkone, Today, 11:37 AM
                        0 responses
                        6 views
                        0 likes
                        Last Post junkone
                        by junkone
                         
                        Started by quantismo, 04-17-2024, 05:13 PM
                        5 responses
                        35 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by proptrade13, Today, 11:06 AM
                        1 response
                        6 views
                        0 likes
                        Last Post NinjaTrader_Clayton  
                        Started by love2code2trade, 04-17-2024, 01:45 PM
                        4 responses
                        34 views
                        0 likes
                        Last Post love2code2trade  
                        Started by cls71, Today, 04:45 AM
                        2 responses
                        10 views
                        0 likes
                        Last Post eDanny
                        by eDanny
                         
                        Working...
                        X