Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How do I programmatically call a buttons OnClick Method ?

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

    How do I programmatically call a buttons OnClick Method ?

    Hello everyone!

    I am looking for working example of calling a buttons click event from a piece of code under "OnBarUpdate"

    my buttons are on the chart, I have tried multiple methods of creating a click event but have been unsuccessful getting it to work in ninjascript mostly because I am still learning.
    Some of these methods are as follows:

    button.PerformClick()

    button_Click(null, new EventArgs());

    InvokeOnClick(button, e);​


    I can provide my .cs file if needed, pointing me in the right direction would be most appreciated!

    Thanks in advance,


    BeachTrader​

    #2
    Hello BeachTrader11807,

    Thanks for your post.

    This would require the use of custom C# / WPF code and goes beyond the support we would be able to provide. You could do research about the Button.OnClick event by doing a Google search for something like "C# Button.OnClick event" for more information about this event.

    You could view the reference sample linked below which demonstrates adding buttons to a custom NinjaScript which you might find helpful.
    https://ninjatrader.com/support/help...ui)-modifi.htm

    This Ecosystem User App Share script also demonstrates using buttons which you may find helpful.
    https://ninjatraderecosystem.com/use...bar-buttons-2/

    This forum thread will also be open for other community members to share their insights on this topic.

    Let me know if I may assist further.


    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      BeachTrader11807 If you have a declared event handler method, it is essentially just another method, albeit one that is enabled to be invoked by an event.

      You mentioned you tried something like "button_Click(null, new EventArgs());". Presumably, "button_Click" is your event handler? That is certainly one approach, but there are certain caveats to a direct invocation like this.

      Event handler methods usually have two parameters: the object in which the event originated (often named as sender), and the event arguments associated with the event (often named as e). Invoking any method with required parameters must include values for those parameters. In the case of event handlers, those are supplied appropriately when the event is triggered. For direct invocation, they are still required, but can be passed as anything that is of the correct type. Typically, null is usable for this purpose, but other correctly-typed items can also be used.

      The caveats, therefore, are simple:
      • Always pass correctly-typed parameters to any method, including event handlers
      • null can be used in most cases where a parameter is of a nullable type -- for event handlers, this is almost always the case
      • However, whatever parameters are passed must be strictly usable in the method as if they came from the event trigger -- this means that if you use those directly-passed parameters (as opposed to event-generated parameters) in your logic, they must be fully compatible with the method's code
      • Any directly-passed parameters should be documented and known, so there is zero opportunity for "unexpected" behaviour
      • If the method parameters are not referenced at all in the method (fairly common), using null is a reasonable way to go ... but always ensure the method logic recognises a direct invocation as opposed to an event triggered invocation
      The general principle is probably: invoke directly if really need be, but always with valid parameters, and have logic in the method to recognise if the invocation is from an event trigger or a direct invocation.

      One final comment. If you need direct invocation, it can often be more effective to create a separate method that executes the actions of the event handler, and that can be directly invoked without concern about event arguments etc. In that case, use the event handler to handle any context-related activities from the triggered event and then invoke the separate method from the event handler to execute appropriate event actions. For direct invocation, simply use the separate method as you would any other. Just another useful approach.

      Thanks.
      Multi-Dimensional Managed Trading
      jeronymite
      NinjaTrader Ecosystem Vendor - Mizpah Software

      Comment


        #4
        Thank you for both of your responses NinjaTrader_BrandonH and jeronymite , I apologize for such a delayed response. The quality of your answer jeronymite is amazing, it gave me a lot to consider and its very apparent I need to keep learning more about C#. I have solved this problem with a simple AutoHotKey script for now, but I may work on it again in the future.

        Thanks again.

        Happy Trading!

        Comment


          #5
          Originally posted by jeronymite View Post
          One final comment. If you need direct invocation, it can often be more effective to create a separate method that executes the actions of the event handler, and that can be directly invoked without concern about event arguments etc. In that case, use the event handler to handle any context-related activities from the triggered event and then invoke the separate method from the event handler to execute appropriate event actions. For direct invocation, simply use the separate method as you would any other. Just another useful approach.
          Yes - that is most likely the way it should be done. One other thing to think about is what thread you're in - for instance, if your onclick event does something with UI resources like buttons or labels etc., and you're trying to call the method from a timer, you may not be in the right thread unless you carefully created the timer in the right thread with dispatchertimer, so you may need to invoke across to a UI thread to be able to do what would otherwise be possible in a UI thread like the one that services clicking on a button.
          Bruce DeVault
          QuantKey Trading Vendor Services
          NinjaTrader Ecosystem Vendor - QuantKey

          Comment


            #6
            This is how I find the chart trader buttons in my addon:

            Code:
            Button buyMktButton = parentWindow.FindFirst("ChartTraderControlQuickBuyMarketButton") as Button;
            Button sellMktButton = parentWindow.FindFirst("ChartTraderControlQuickSellMarketButton") as Button;​
            Then I use WPF automation Ids to invoke them like this:
            Code:
            using System.Windows.Automation;
            using System.Windows.Automation.Peers;
            using System.Windows.Automation.Provider;​
            
            ...
            ...
            ButtonAutomationPeer buyMktButtonPeer = new ButtonAutomationPeer(buyMktButton);
            IInvokeProvider buyMktButtonInvokeP = buyMktButtonPeer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
            buyMktButtonInvokeP.Invoke();​
            ...


            See here for reference: [REDACTED]​
            Last edited by NinjaTrader_BrandonH; 08-20-2023, 03:00 PM.

            Comment


              #7
              Hello skipper-flatiron-18,

              Thanks for your notes.

              I have removed the third-party link you shared as we do not allow promotional 3rd party links on our forum.
              <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

              Comment


                #8
                NinjaTrader_BrandonH It's an Stackoverflow developer question page link. Do you know what Stackoverflow is? Your entire ecosystem of developers use it every single day. So adding a link to stackoverflow helps people learn and it's not thrid-party promotional link. Please relax your rules a bit

                Comment


                  #9
                  Hopefully just an oversight, skipper-flatiron-18. StackOverflow postings are one of the major "GoTo" resources for developers and have been posted elsewhere in the Forums often. Looking forward to seeing the original link restored and taking a look there. Appreciate your posting.

                  Thanks.
                  Multi-Dimensional Managed Trading
                  jeronymite
                  NinjaTrader Ecosystem Vendor - Mizpah Software

                  Comment


                    #10
                    Weird response by NT to a development thread.

                    Comment


                      #11
                      Originally posted by NinjaTrader_BrandonH View Post
                      Hello skipper-flatiron-18,

                      Thanks for your notes.

                      I have removed the third-party link you shared as we do not allow promotional 3rd party links on our forum.
                      How is StackOverFlow promotional?
                      It's a universal resource, free to use and quite vital to non programmers like myself?
                      I think you should review this policy a little.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      558 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      324 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      101 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      545 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      547 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X