Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Toggle off and on drawn.rectangles within an indicator with a shortcut key.

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

    Toggle off and on drawn.rectangles within an indicator with a shortcut key.

    I've written an indicator and want to be able to toggle turning off and on some drawn.rectangles the indicator automatically draws.

    I've already tried repurposing the code from ABToolbars and read the discussion here:
    https://forum.ninjatrader.com/forum/...ator-show-hide

    I tried adding to public class:
    Code:
    private bool isDrawRectangleVisible = true;
    private Chart chartWindow; // Declare chartWindow at the class level
    and


    Code:
    private void OnKeyUp(object sender, KeyEventArgs e)
    {
    if (e.Key == Key.D && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
    {
    ToggleDrawsVisibility();
    }
    }
    
    private void ToggleDrawsVisibility()
    {
    foreach (var obj in chartWindow.ActiveChartControl.ChartObjects)
    {
    var draw = obj as DrawingTool;
    if (draw != null && draw.IsUserDrawn &&
    (draw.Name.Contains("Top Cluster") || draw.Name.Contains("Bottom Cluster")))
    {
    draw.IsVisible = !draw.IsVisible; // Toggle visibility
    }
    }
    
    chartWindow.ActiveChartControl.InvalidateVisual();
    ForceRefresh();
    }
    ​
    But I'm still very new to coding and not sure how to access drawn objects within the indicator for visibility off and on.

    Any advice would be much appreciated.

    #2
    Hello davydhnz,

    The code you provided appears to be doing that. Is there some issue with what you are currently trying? The drawing objects can be found in the chart objects collection or the draw objects collection.
    https://ninjatrader.com/support/help...ghlightsub=dra wobjects

    On a side note calling InvalidateVisual is not suggested to be used, this can lead to freezing.

    Warning: You should only call ForceRefresh() if the chart truly needs to be visually updated. It is NOT recommended to invalidate the chart control directly as this could cause issues with threading which result in dead locks.

    Comment


      #3
      Thank you for your advice NinjaTrader_Jesse

      I changed it to:


      Code:
      private Dictionary<Key, string> shortcutTags = new Dictionary<Key, string>
      {
      { Key.D, "MyTag2" },
      { Key.W, "MyTag4" }, // Additional shortcut key and tag
      };
      
      private void OnKeyUp(object sender, KeyEventArgs e)
      {
      if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) && shortcutTags.ContainsKey(e.Key))
      {
      ToggleDrawsVisibility(shortcutTags[e.Key]);
      }
      }
      
      private void ToggleDrawsVisibility(string targetTag)
      {
      foreach (var obj in DrawObjects)
      {
      var draw = obj as DrawingTool;
      if (draw != null && !draw.IsUserDrawn && draw.Tag.Contains(targetTag))
      {
      draw.IsVisible = !draw.IsVisible; // Toggle visibility
      Print("Toggled visibility for: " + draw.Name + " - Tag: " + draw.Tag);
      }
      }
      
      
      }
      And removed InvalidateVisual, but need ForceRefresh or I'll have to move the chart to see the change.

      But when using the code on OnRender, I noticed ForceRefresh isn't enough, but InvalidateVisual without ForceRefresh works,
      but you said "InvalidateVisual is not suggested to be used, this can lead to freezing."

      So is there any other way to toggle OnRender apart from InvalidateVisual that works?
      Last edited by davydhnz; 08-12-2023, 11:37 PM. Reason: Trial and error results.

      Comment


        #4
        Hello davydhnz,

        You could add and remove a drawing object immediately after adding it, removing an object will force the chart to update.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        649 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        370 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        109 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        574 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        576 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X