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

How to to get \ set buttons properties (IsEnabled, Content, Background, etc)?

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

    How to to get \ set buttons properties (IsEnabled, Content, Background, etc)?

    Hello.

    I add some buttons to strategy accordingly to this manual sample: http://ninjatrader.com/support/helpG...th_buttons.htm

    When I try to set \ get properties from within OnBarUplate() by this way:
    PHP Code:
    if( Btn_Obj.Content == "Allow Long"EnterLong(); 
    I got error "The calling thread cannot access this object because a different thread owns it."

    How to do get buttons properties in proper way?
    How to do set buttons properties in proper way?

    (I searched for samples, but didn't find examples of utilizing buttons)
    fx.practic
    NinjaTrader Ecosystem Vendor - fx.practic

    #2
    I tried to make additional global bool variable, change it value in Button_Click() handler, and use it value inside OnBarUpdate().

    It looks like working approach, but need additional entity.

    May be, there is way to get information from buttons directly?
    Last edited by fx.practic; 08-10-2017, 05:57 AM.
    fx.practic
    NinjaTrader Ecosystem Vendor - fx.practic

    Comment


      #3
      Hello,

      Thank you for the post.

      In this situation, accessing UI elements directly is not really suggested. The correct approach would be to have the button's event control a property rather than trying to access a UI element in your code. Your changes are likely what I would suggest.

      Can you tell me what do you mean by an additional entity? Do you mean another state to the button?



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

      Comment


        #4
        Thank You for answer. Let me clarify and ask more questions on same.

        Now I use something like this:

        PHP Code:
        bool Is_Button_Enable_Action false;

        OnBarUpdate()
        {
            if( 
        Is_Button_Enable_Action Do_Some_Action();
        }

        private 
        void Btn_Obj_Click(object senderEventArgs e)
        {    
            if( 
        Btn_Obj == null ) return;
            
        ChartControl.Dispatcher.Invoke((Action)(() =>
            {
                
        Is_Button_Enable_Action true;
            }));


        Under additional entity I mean Is_Button_Enable_Action variable.

        Question 1: Is code above correct way to get info from button?
        Question 2: If I want to change state of button, or some button properties, is code below correct way?
        PHP Code:
        OnBarUpdate()
        {
            if( 
        Btn_Obj != null 
            {
                
        ChartControl.Dispatcher.Invoke((Action)(() =>
                {
                    
        Button_Obj.IsEnable true;
                    
        Button_Obj.IsEnable false;
                    
        Button_Obj.Background Brushes.Red;

                }));
            }

        fx.practic
        NinjaTrader Ecosystem Vendor - fx.practic

        Comment


          #5
          Hello,

          Thank you for the reply.

          In the first sample, you should not need to use the Dispatcher if you are only setting the boolean value, you should be able to remove this. If you are getting a threading error by setting the bool you would need to use the Dispatcher but please do not use Invoke specifically, use InvokeAsync instead. Invoke is known to cause deadlocks:


          Aside from not using Invoke, everything else looks correct as far as setting a value would go.

          Regarding the second example, this would also depend if you are getting a threading error. In the case that the handler is accessing the button object from another thread, you would need to use the dispatcher. Generally, you can locate what does and does not need dispatching by just trying the code without it. If there is no threading error that should be ok to use. This would also need to change to InvokeAsync instead of Invoke.

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

          Comment


            #6
            Button for drawing Horizontal lines

            I am trying to place a button on the top above the chart that would show/hide Draw.Line(s)

            Would like to use the code example from earlier in this post. But ran into a problem figuring out the last part of this line:

            if( Is_Button_Enable_Action ) Do_Some_Action();

            The action would be:

            Draw.HorizontalLine(this, "RangeMax", false, MAX(High, rangeBars)[0], Brushes.Gold, DashStyleHelper.DashDotDot, 3);

            And then to hide it. May I request assistance in what I would place as "Do Some Action();"

            So I think my code would look something like this:


            OnBarUpdate()
            {
            if( Is_Button_Enable_Action )

            Draw.HorizontalLine(this, "RangeMax", false, MAX(High, rangeBars)[0], Brushes.Gold, DashStyleHelper.DashDotDot, 3);
            }

            Unfortunately, NT8 does not like that there is no "Do Some Action();"

            Please advise me.

            Also once I have that, is the private void in the onbarupdate section or on its own section?

            Comment


              #7
              Hello,

              Thank you for the reply.

              It looks like in the earlier post Do_Some_Action() was used as a placeholder, or is a method name in that specific script. You would need to define your own method and replace Do_Some_Action() with that methods name.

              If you are unsure on how to make a method, I would suggest reviewing some C# tutorials surrounding Methods. A quick search could be "C# void methods" https://docs.microsoft.com/en-us/dot...tructs/methods

              A void method would never go inside of another method like OnBarUpdate, you would need to place the other methods you create outside of OnBarUpdate within the Class.

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

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by cre8able, Today, 03:20 PM
              0 responses
              5 views
              0 likes
              Last Post cre8able  
              Started by Fran888, 02-16-2024, 10:48 AM
              3 responses
              47 views
              0 likes
              Last Post Sam2515
              by Sam2515
               
              Started by martin70, 03-24-2023, 04:58 AM
              15 responses
              114 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by The_Sec, Today, 02:29 PM
              1 response
              8 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by jeronymite, 04-12-2024, 04:26 PM
              2 responses
              32 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Working...
              X