Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Removing DrawObjects of specific type

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

    Removing DrawObjects of specific type

    Hi,
    is there a way to remove DrawTextObjects programmatically, without removing DrawObjects of another type.
    I have TextObjects drawn on the chart, which are "tagged" when running the indicator. As the tags are written, when the indicator is running, it is difficult to remove them by the tags. The tags are build by a "name" + pricelevels which change.
    Can I remove the Objects by saying something like - all drawing tools which tags start with "name". Or is it possible to remove only DrawTextObjects and not Objects of another type?

    #2
    Hello keepsimple,

    Thank you for your post.

    Using the IDrawObject we can search the Tag string for StartsWith(), for example:
    Code:
    			foreach(IDrawObject draw in DrawObjects)
    			{
    				if(draw.Tag.StartsWith("name"))
    				{
    					RemoveDrawObject(draw);
    				}
    			}
    For information on IDrawObject please visit the following link: http://www.ninjatrader.com/support/h...drawobject.htm

    For information on StartsWith() please visit the following link: http://msdn.microsoft.com/en-us/library/ms228630.aspx

    Please let me know if I may be of further assistance.

    Comment


      #3
      Hi Patrick,
      thanks for quick response.
      Problem is I add TextDraw Objects if price reaches a new pricelevel.
      For my understanding I need for every IDrawObject a variable. I cannot declare variables in front, because I do not know how many I whould need, and if I knew, there whould be a lot of them.
      My code does something like:
      if new pricellevel {
      DrawText("Name" + pricelevel, ...........)
      }
      Perhaps it whould work the other way around to remove text and keep other types of drawings.

      Comment


        #4
        Hello keepsimple,

        There are a couple of ways to accomplish what you are looking for.

        If you had an idea that you only wanted to keep say 20 (of the same type) drawtext objects on the chart you can use a simple counter to 20 and then start over at 1. This will automatically remove older drawtexts as you add new drawtexts. You could even make the 20 a variable that you can enter on the indicator in case you wanted to vary the number of drawtext objects. Something like:

        private int textObjs = 20;

        if (textObjs >= 20) textObjs = 1; // reset counter

        Drawtext ("Name"+textObjs,....) // create a new object and remove the old one.

        textObjs++ ; // Increment counter



        Another idea is to keep a running number counter, like above and store the price value with each number and then later determine which objects to remove by price level. This would involve array processing and comparative logic. A lot more work but could be done.

        Please let me know if I can be of further assistance.

        Comment


          #5
          Thanks Paul,
          this might help, will work with that.

          Regards

          Comment


            #6
            Hi,
            I followed Patricks advice.
            I created a button, which includes this code:

            foreach(IDrawObject draw in DrawObjects)
            {
            if(draw.Tag.StartsWith("Name"))
            {
            Print(draw.Tag);
            RemoveDrawObject(draw.Tag);
            ChartControl.ChartPanel.Refresh();
            }
            }

            I get the desired DrawObjects printed to the Output Window with the tags they have.
            But it removes only ONE Drawobject and not all Objects listed in the Output Window,
            So how can I solve that, or what is wrong?

            Comment


              #7
              Hello keepsimple,

              If you want to attach your complete code I could test out and advise.

              Comment


                #8
                Figured its an enumeration problem, thanks

                Comment


                  #9
                  Hi KeepSimple
                  Sorry to dig up an old thread. I am faced with the same issue that you described in post #6 below. Then you said that you "Figured its an enumeration problem" Please can you post your solution to this?

                  many thanks

                  Comment


                    #10
                    Hi,
                    basically the problem is, at least in my understanding, that removing objects messes up the iteration through the loop. so if you want to keep the logic, you have to find a solution for that and probably have to change the syntax for the iteration. i solved my problem by changing the plot not removing it, which was a better solution for me at the end for several reasons. But to come back to your question, the cause for the error is a messed up iteration through the loop.

                    Regards

                    Comment


                      #11
                      Thanks for your swift reply. That is useful. I discovered some "odd" behaviour from using this block of code.

                      Code:
                      foreach( ChartObject o in ChartControl.ChartObjects )
                                      { 
                                          if( o.DrawType == DrawType.Text )
                                          { 
                                              IText txt = (IText)o;  Print("tag:" + o.Tag + " | txt: " + txt.Text ); 
                                              //RemoveDrawObject(o.Tag);
                                          }
                                      }
                      Iterating the ChartObjects in this loop does indeed return all the drawObjects. However using RemoveDrawObject removes the first item and does not iterate the rest of the list. Clearly i don't understand how this works. But your comments are very useful to me. Thanks again.

                      Comment


                        #12
                        Hello,

                        The enumeration error is caused when removing an object from the collection. Here is one way to go through and remove all the (drawn by script) text objects on the chart. (name is a local string).

                        Code:
                        for (int i = 0; i < DrawObjects.Count; i++)	
                        {
                        	foreach(IDrawObject o in DrawObjects)
                                   	{ 
                                      	      if( o.DrawType == DrawType.Text )
                                       		 { 
                                           	    name = o.Tag;
                        			     break;
                                        	 } 
                                           } 
                        		if (name != "")
                        		    {
                        			RemoveDrawObject(name);
                        		     }
                         }

                        Comment


                          #13
                          Thanks Paul, I appreciate your prompt reply. Prior to posting my issue, I had attempted to RemoveDrawObjects by looping through the "DrawObjects.Count". However i could not immediately see how to then iterate (and remove) the objects by name. I believe your code example has answered that question.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          633 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          364 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
                          567 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          568 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X