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

AddOnFramework.cs StateChange when Tab is created and Removed?

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

    AddOnFramework.cs StateChange when Tab is created and Removed?

    Hi,
    I want to add StreamWriter capacity to the AddOnFramework sample, but I am having trouble understanding what is a tab's equivalent event when it is first created.
    I've read about Window creation events, but I have not found any for a tab creation event.

    From my understanding, when writing an indicator, I can initialize objects like StreamWriter during the State change "DataLoaded"

    Intellisense suggests State.Terminated, but I don't see a state.Created etc

    It seems a Tab has a Cleanup() event ...

    I've also read about TabControl() but that does not seem the place...
    unless tc.Inititalized() can be used ?


    Finally, I have tried

    Code:
    public AddOnFrameworkTab()  // method inside class of same name
    {
        // ....
        StWr = new StreamWriter(StWrFilePath, false);
    and that seemed to pass compilation, and it created the file for StreamWriter..

    and Cleanup() is where I can dispose StWr
    Code:
    if (StWr != null)
    {
    StWr.Close();
    StWr.Dispose();
    StWr = null;
    }
    but there is also tc.Unloaded() ... ?


    Next Question, when I have multiple Tabs in a window, and I want to get an id for each, is there a collection in the Window manager that can tell me how many tabs there are?

    Also, when I tried to use the property TabName, or this.GetHashCode() from inside AddOnFrameworkTab, it errored with Obj ref not instance...
    so it appaers I need to wait for an event to complete which indicates that the tab has been created, and then I can get access to it's properties?
    so I would need to understand what event I can override to gain access to those?

    Thanks...

    #2
    Hello balltrader,

    For events after a new tab is opened add an event handler to the NTTabPage class Loaded event in the constructor.
    For events after a tab is closed add code to the Cleanup override.

    For events after the window is opened add an event handler to the NTWindow class Loaded event in the constructor.
    For events after the window is closed add an event handler to the Closed event in the constructor.

    The <TabControl>.Items.Count is the count for the number of tabs.

    GetHeaderPart() is where the tab name can be set.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the link to your tips.

      I'm hacking into the jungle.

      after the event fires and inside my function,
      how do I get access to the properties of the instance?

      Code:
      // inside constructor
      // make this object (tab) when event named Loaded fires, call the custom class function Tab_Created
      this.Loaded += Tab_Created;
      
      
      // custom function
      private void Tab_Created(object sender, RoutedEventArgs e)
      {
      // what is object type?
      //NinjaTrader.Code.Output.Process("tab created, sender type= " +sender.GetType() , PrintTo.OutputTab2);
      //NinjaTrader.Gui.NinjaScript.AddOnFrameworkTab
      
      // assign local var to object type
      AddOnFrameworkTab thisTab = sender as AddOnFrameworkTab;
      
      // now can you access a property of it?
      NinjaTrader.Code.Output.Process("tab opened, properties " + thisTab.Properties.ToString() , PrintTo.OutputTab2);
      
      // error   obj ref not set to instance
      
      }

      thanks

      Comment


        #4
        Hello balltrader,

        The tab class that is opened has loaded event. In an event handler assigned to loaded type this. and you will get a list of properties available to the tab instance.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I discovered I can't use the Event Handler "Loaded" because it fires every time a tab gains focus.
          For explanation, if I add a new tab, then click on the first tab, the event "Loaded" fires again.

          I used Intellisense to scroll through all the properties of the tab, looking for events I might be able to use, but I could not find anything that would fire when a new tab was created, and only then.

          I tried the event handler for Initializing but it did not call my function.

          My task was to give a unique name to the StreamWriter file based on the tab index, so when there are multiple tabs opened, each tab has it's own streamwriter file.

          But I can't figure out how to get that done using an event.


          Maybe I should try getting a reference to the newly created tab page, from inside CreateTabPage ?


          Code:
          // INTTabFactory member. Required to create tabs
          public NTTabPage CreateTabPage(string typeName, bool isTrue)
          {
          return new AddOnFrameworkTab();
          
          // instead of returning it above,
          // COULD I assign it to a local var, so I can get it's instance,
          // then access the properties of that instance, like TabName , TabIndex , Instrument...
          // then set the filename for the streamwriter inside here?
          // then return using the local var which references the new tab?
          
          
          }

          Comment


            #6
            Hello balltrader,

            The constructor of the tab should be called each time its created, in the tab control that would happen when changing tabs. If you are trying check if a file exists or create it you could add some logic to your loaded event so it happens only when necessary.

            You could use if(File.Exists()) as one path forward, if the file exists don't run your streamwriter logic and just use the file instead.

            For the naming you may want to avoid using an index because tabs can be re arranged, you may want to instead access something within that tab or add a public property of your own to generate a unique ID for each tab that needs unique files. Relying on the tab managers indexes may fail if you sort the tabs or if any other change happens to the tab order.

            I look forward to being of further assistance.

            JesseNinjaTrader Customer Service

            Comment


              #7
              please clarify "in the tab control that would happen when changing tabs"...

              do you mean when a background tab is made foreground by user mouse, Ninjscript "Creates" that tab again?

              does that mean background tabs are not really there and cannot be running active code?


              anyway, back to my original topic about Streamwriter FIlename, I'm abandoning this idea of using an event on new tab creation and just using a timestamp for now.


              I tested the AddOn named AddonShell from Chelsea's link (https://ninjatrader.com/support/foru...egy#post759830)
              and I noticed each new tab has a name with an incrementing number, and tht does not change when you re-arrange the order of the tabs.

              So how is that incrementing number being made? I don't see any code in the method GetHeaderPart which would increment it, so is it magically adding an incremental number like the operating system adds numbers to duplicate files? And when you mention TabManager TabIndex may change depending on the order (position) of the tabs, it appears this auto-naming scheme is not relying on the TabIndex property since it doesn't change when the tab is dragged.



              Comment


                #8
                Hello balltrader,

                please clarify "in the tab control that would happen when changing tabs"...
                If you change tabs, for example clicking a different tab to navigate aware from the current tab.

                do you mean when a background tab is made foreground by user mouse, NinjaScript "Creates" that tab again?
                It will call the constructor again to generate the tab content, the instance would remain in memory though.

                does that mean background tabs are not really there and cannot be running active code?
                no

                For the tab name/number I wouldn't suggest using any of the tab controls values and instead generate your own ID for your use case. That way you could save that ID as part of the IWorkspacePersistence object. I am not aware of any documentation for the various properties in the tab control.

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

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by bortz, 11-06-2023, 08:04 AM
                47 responses
                1,607 views
                0 likes
                Last Post aligator  
                Started by jaybedreamin, Today, 05:56 PM
                0 responses
                9 views
                0 likes
                Last Post jaybedreamin  
                Started by DJ888, 04-16-2024, 06:09 PM
                6 responses
                19 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by Jon17, Today, 04:33 PM
                0 responses
                6 views
                0 likes
                Last Post Jon17
                by Jon17
                 
                Started by Javierw.ok, Today, 04:12 PM
                0 responses
                15 views
                0 likes
                Last Post Javierw.ok  
                Working...
                X