Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Change Color of selected Rectangle

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

    Change Color of selected Rectangle

    I want to change the color from a selected rectangle. Therefore I use a click event to run the following void.
    It works fine, but the changes are not saved. If I Reload NinjaSkript via F5 the old color appears.

    private void changeColorRectangle()
    {
    foreach (var obj in chartWindow.ActiveChartControl.ChartObjects)
    {
    var draw = obj as DrawingTool;
    if (draw != null)
    {
    if (draw.IsUserDrawn && draw.IsSelected)
    {
    if (draw.Name == "Rectangle")
    {
    NinjaTrader.NinjaScript.DrawingTools.Rectangle RT;
    foreach (Gui.NinjaScript.IChartObject thisObject in ChartPanel.ChartObjects)
    {
    RT = thisObject as NinjaTrader.NinjaScript.DrawingTools.Rectangle;
    if(thisObject is NinjaTrader.NinjaScript.DrawingTools.Rectangle)
    {
    if (RT.IsSelected)
    {
    //RT.AreaOpacity =40;
    RT.AreaBrush = Brushes.Red;
    RT.OutlineStroke.Brush = Brushes.Red;
    }
    }
    }
    }
    }
    }
    }
    chartWindow.ActiveChartControl.InvalidateVisual();
    ForceRefresh();
    }

    #2
    Hello Goldy,

    Thanks for your post.

    First, we would advise not to call chartWindow.ActiveChartControl.InvalidateVisual(); as that can cause lockups.

    Testing your example code here and changing to not use chartWindow.ActiveChartControl.and just use ChartControl, it changes the rectangle and it remains that color when reloading Ninjascript.

    Comment


      #3
      Hello Paul,

      thanks for your post.
      No change if I don't use chartWindow.ActiveChartControl.
      I don't understand how to use just ChartControl. Just using ChartControl() I get compiling error CS0118.

      The only way I found is to click at "Drawing Objects ..." and leave the window witk OK-Button. This action stores all changes permanently.
      How can I do this in code?

      Btw:
      I found the same issues with the exemple of the helpGuides:
      //https://ninjatrader.com/support/helpGuides/nt8/?drawingtools_drawobjects.htm

      Best regards
      Goldy
      Last edited by Goldy; 01-29-2021, 12:20 PM.

      Comment


        #4
        Hello Goldy,

        Thanks for your reply.

        I am assuming you are doing this in an indicator.

        Please test this:

        private void changeColorRectangle()
        {
        foreach (var obj in ChartControl.ChartObjects)
        {var draw = obj as DrawingTool;
        if (draw != null)
        {
        if (draw.IsUserDrawn && draw.IsSelected)
        {
        if (draw.Name == "Rectangle")
        {
        NinjaTrader.NinjaScript.DrawingTools.Rectangle RT;
        foreach (Gui.NinjaScript.IChartObject thisObject in ChartPanel.ChartObjects)
        {
        RT = thisObject as NinjaTrader.NinjaScript.DrawingTools.Rectangle;
        if(thisObject is NinjaTrader.NinjaScript.DrawingTools.Rectangle)
        {
        if (RT.IsSelected)
        {
        //RT.AreaOpacity =40;
        RT.AreaBrush = Brushes.Red;
        RT.OutlineStroke.Brush = Brushes.Red;
        }
        }
        }
        }
        }
        }
        }

        ForceRefresh();
        }




        Comment


          #5
          Hello Paul,

          that's right.

          Then I mark any rectangle and with the shortcut RightAlt + C
          see code:
          if (e.Key == Key.C && (Keyboard.IsKeyDown(Key.RightAlt) || Keyboard.IsKeyDown(Key.RightCtrl))) changeColorRectangle();


          I change the color to red / red.
          This works fine temporaly but no permanently store.

          Best regards
          Goldy

          Comment


            #6
            Hello Goldy,

            Thanks for your reply.

            Please let me know if you are able to successfully use the test sample code (your code reworked) in post #4.

            Comment


              #7
              Hello Paul,

              yes, i copied the code from your post #4 and pasted it into my program so that no transmission errors can occur there. With that I found the same errors.

              I also tested the code with another computer so that hardware and/or configuration errors can be excluded.

              Best regards
              Goldy

              Comment


                #8
                Hello Goldy,

                Thanks for your reply.

                The behavior you see is different than what I am seeing here with the test code. The rectangle is turned red and when Ninjascript is reloaded the rectangle remains Red. When the workspace with the chart with the Red rectangle is saved and Ninjatrader is restarted, the chart loads with the Red rectangle.

                You may want to try the test code in a new indicator to isolate it from your existing code to see if you then achieve the functionality I described.

                What version of NinjaTrader8 are you using? (This can be found under help>About)

                Comment


                  #9
                  Hello Paul,

                  thanks for your reply.

                  I am using the newest version: 8.0.23.2 64-bit

                  I've tried the code several times in another indicator. I can't imagine it, but maybe it's because of my mouse click control. I would like to test your complete code.

                  Would you please make it available to me?

                  Best regards
                  Goldy

                  Comment


                    #10
                    Hello Goldy,

                    Thanks for your reply.

                    As requested:

                    test.zip

                    Comment


                      #11
                      Hello Paul,

                      thanks for your replay.

                      It works but I don't understand:
                      If I call the void from OnBarUpdate() the values are stored.
                      and if I call the voids via shortcuts the values don't be stored.

                      Please look attached zip-file.

                      Best regards
                      Goldy


                      Attached Files

                      Comment


                        #12
                        Hello Goldy,

                        Thanks for your reply.

                        I tested your example as provided and I can change the rectangle to the two colors and the colors persist through a reload of NS and do save and reload when the workspace is saved. I asked a colleague to test as well and he reported the same.

                        Comment


                          #13
                          Hello Paul,

                          thanks for your reply and for your effort!

                          Now I found the reason for the different behavior.
                          I use rectangle globally (Drawin Objects Genaral Attach to All charts).

                          In this case the changes will not be stored.

                          How can I force the code to store also in this case?

                          Best regards
                          Goldy



                          Comment


                            #14
                            Hello Goldy,

                            Thanks for your reply.

                            Being a global object does change the picture.

                            There does not appear to be a way to accomplish your goal and we have a feature request identified as SFT-4438, "Propagate NS changes to a global drawing object to all charts" and we will certainly add your vote to that.

                            Something that was just suggested that you could try would be to pull the details of the rectangle, remove the rectangle, and then recreate the rectangle using the IsGlobal overload.

                            Comment


                              #15
                              Hello Paul,

                              thanks for your replay.

                              Remove and recreate would be easy.
                              But how can I make it userDrawn?

                              Best regards
                              Goldy

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              624 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              359 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              105 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              562 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              567 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X