Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Change button colors for clicked and not clicked

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

    Change button colors for clicked and not clicked

    I found the code posted by NinaTrader_ZacharyG and it's exactly what I needed! I couldn't have accomplished this on my own and I'm extremely grateful for the generosity.
    One small modification I'd like to make is to change the button color when it's clicked. Does anyone have a snippet of code showing how this is accomplished when defining button characteristics in
    new System.Windows.Controls.Button?
    I'm not sure it can be done, but I guess it doesn't hurt to ask.

    #2
    Hello Doctor JR,

    Thank you for your post.

    Yes, you can modify the button background color when it's clicked. Here's a fairly simple example script that adds some EMAs to a chart and toggles them on and off using buttons at the top of the screen - the button background color changes depending on whether or not the button's been clicked.

    Please let us know if we may be of further assistance to you.
    Attached Files
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Simple solution that works great. Thank you!!

      Comment


        #4
        Thanks again for that information as I need a vivid visual reminder that a button has been clicked and needs to be returned to the off position to prevent an unwanted repeat event on subsequent onBarUpdates. To idiot-proof against that I return each button to a "Clicked = off" default position at the beginning of onBarUpdate. Then the actual chart button must be physically returned to "off" before it can again do it's "Clicked = true" task. I suppose that would be a chart button best practice, but you probably already know that.

        One more question on the chart button topic: I'd like to apply some right margin to a couple of buttons to provide visual separation from buttons producing different categories of operations. I see margin gets some discussion on an MS site discussing forms but I'm still a long way from knowing the proper syntax to actually do it with my chart buttons. Assuming this can be done, do you have any samples of margin being applied to chart buttons?

        Thanks.

        Comment


          #5
          Hello Doctor JR,

          Thank you for your reply.

          You can add margin to a button by specifying it's Margin property:

          Button myButton1 = new Button();
          myButton1.Margin = new Thickness(0, 10, 0, 10);
          myButton1.Content = "Button 1";

          The thickness values start at the left and go around clockwise, so the first 0 would be the left margin, the 10 in that example would be top margin, the second 0 the right margin value, and the final 10 the top margin value.

          So for example if I wanted to add a 10 px right margin to my first EMA button in the example I posted above, I could do this after creating the button:

          ema1Button.Margin = new Thickness(0, 0, 10, 0);

          Further information on setting margins may be found in Microsoft's publicly accessible documentation here:

          Learn about HorizontalAlignment, Margin, Padding, and VerticalAlignment, which control child element position in Windows Presentation Foundation applications.


          Please let us know if we may be of further assistance to you.
          Kate W.NinjaTrader Customer Service

          Comment


            #6
            How nice to fix my issues by plugging in one short and simple line of code. It works as you've advertised. Thank you.

            Comment


              #7
              Originally posted by NinjaTrader_Kate View Post
              Hello Doctor JR,

              Thank you for your post.

              Yes, you can modify the button background color when it's clicked. Here's a fairly simple example script that adds some EMAs to a chart and toggles them on and off using buttons at the top of the screen - the button background color changes depending on whether or not the button's been clicked.

              Please let us know if we may be of further assistance to you.

              Hi Kate,

              I have an indicator that has similar buttons as you posted. The indicator draws an AutoBuy toggle button whereby when I click, the button is greyed out and text changed to "Off AutoBuy".
              My AutoBuy variable is set to true, and the indicator will listen for buy conditions and enter long if these conditions are met.
              If I click again, the button's original color and text is restored and AutoBuy variable is set to false.

              The indicator automatically sets the AutoBuy variable to false if the buy conditions are not met after a few bars have passed. This is a safety feature that prevents wrong entries should the market move too quickly, and manual clicking the button to toggle it off is not fast enough.

              However, the problem is if the indicator automatically sets the AutoBuy variable to false, the button remains greyed out. I have to manually click it to restore its original background color and text.

              Is there a way to restore the background color and text of the button using Ninjascript without clicking on the button?

              You assistance is much appreciated.

              Comment


                #8
                Hello Rainmakersg,

                Thanks for your note.

                "the problem is if the indicator automatically sets the AutoBuy variable to false, the button remains greyed out"

                You may try setting the button Brush color in your script to a different color at the same time you are setting your AutoBuy variable to false so that the button Brush color changes when the AutoBuy variable gets set to false.

                Please let me know if I may assist further.
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_BrandonH View Post
                  Hello Rainmakersg,

                  Thanks for your note.

                  "the problem is if the indicator automatically sets the AutoBuy variable to false, the button remains greyed out"

                  You may try setting the button Brush color in your script to a different color at the same time you are setting your AutoBuy variable to false so that the button Brush color changes when the AutoBuy variable gets set to false.

                  Please let me know if I may assist further.
                  Hi Brandon,

                  Thanks for your reply. This is my exact question, which is how to change the color of the button using the script WITHOUT running the click event. I only know how to change the color of a button with a click event. If it can be changed without a click event, I would appreciate it if you could provide an example of how to do it.

                  Thanks a lot.

                  Comment


                    #10
                    Hello Rainmakersg,

                    Thanks for your note.

                    If you are wanting to change a button color from OnBarUpdate() then a dispatcher would need to be used.

                    We do not have a reference sample available demonstrating how to use a C# dispatcher to accomplish this.

                    This help guide page talks about using a dispatcher in a NinjaScript to access objects on the UI from a NinjaScript objects calling event thread.


                    For more information about using a dispatcher in C#, you could do a Google search for something like 'Dispatcher class C#'.

                    Let me know if I may assist further.​
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_BrandonH View Post
                      Hello Rainmakersg,

                      Thanks for your note.

                      If you are wanting to change a button color from OnBarUpdate() then a dispatcher would need to be used.

                      We do not have a reference sample available demonstrating how to use a C# dispatcher to accomplish this.

                      This help guide page talks about using a dispatcher in a NinjaScript to access objects on the UI from a NinjaScript objects calling event thread.
                      https://ninjatrader.com/support/help...-threading.htm

                      For more information about using a dispatcher in C#, you could do a Google search for something like 'Dispatcher class C#'.

                      Let me know if I may assist further.​
                      Hi Bradon,

                      Thanks for your reply. Google didn't give me a good answer, so I switched to Bing, as it is now incorporated with ChatGPT. It gave me the code and it works!

                      Here it is:

                      HTML Code:
                                              [Button Name].Dispatcher.Invoke(() =>
                                              {
                                                  [Button Name].Background = Brushes.Green;
                                              });​

                      Comment


                        #12
                        Hello Rainmakersg,

                        Important, please use Dispatcher.InvokeAsync() instead of Dispatch.Invoke to prevent the entire UI from being locked.

                        See the warnings in the help guide on the following pages linked below.

                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hi Chelsea, thanks so much for the advice

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by judysamnt7, 03-13-2023, 09:11 AM
                          4 responses
                          55 views
                          0 likes
                          Last Post DynamicTest  
                          Started by ScottWalsh, Today, 06:52 PM
                          4 responses
                          35 views
                          0 likes
                          Last Post ScottWalsh  
                          Started by olisav57, Today, 07:39 PM
                          0 responses
                          7 views
                          0 likes
                          Last Post olisav57  
                          Started by trilliantrader, Today, 03:01 PM
                          2 responses
                          19 views
                          0 likes
                          Last Post helpwanted  
                          Started by cre8able, Today, 07:24 PM
                          0 responses
                          8 views
                          0 likes
                          Last Post cre8able  
                          Working...
                          X