Download MetaTrader 5

Best ways to kill Panel and other objects on the panel completely ?

To add comments, please log in or register
Young Ho Seo
24011
Young Ho Seo  

Dear Traders and Coders

I am currently playing around with Panel for sometime.

I found that Panel object is little bit clumsy at the moment.

I created panel with several buttons and labels on the panel.

When the    "int OnInit()" function takes place (for example, when new input variable set by users), I want to remove the panels and the objects on the panel completely from the chart to create brand new panel.

I am currently using this code line below. I get sucess when I call  "int OnInit()" function" for first time but when I repeat it couple of times then My panel become really ugrl looking with several other panels stacked on top of each other.

 

m_panel.Destroy();


See the screenshot below for better understanding.



I tried following code as well but it did not work at all.


ObjectDelete(0, "Panel");


So How to remove the existing panel completely and create a brand new panel ?

Your ideas will be really appreciated.


Kind regards.

Biantoro Kunarto
18347
Biantoro Kunarto  

You can use ObjectsDeleteAll()

ObjectsDeleteAll(0,0,-1);
Young Ho Seo
24011
Young Ho Seo  
biantoro kunarto:

You can use ObjectsDeleteAll()

Good thinking. 

Just one limitation with this method is that it will delete all the other objects too. 

For example I have lot of lines drawn on chart and I need to keep all the other objects. 

I only want to kill the panel and the objects created for panel.

The problem is that many other objects is created together when the panel is created. It seems the name of the other object is almost randomly and programmatically asigned.

So it is difficult to remove all these objects using ObjectDeleteAll


Kind regards.


Marco vd Heijden
Moderator
5786
Marco vd Heijden  

a better way can be to first check if the object exists before creating it.

then when the ea gets re-initialized, it will see that the object already exists, and skip creation so there will never be any duplicates.

Young Ho Seo
24011
Young Ho Seo  
Marco vd Heijden:

a better way can be to first check if the object exists before creating it.

then when the ea gets re-initialized, it will see that the object already exists, and skip creation so there will never be any duplicates.


I was looking for such a way too. But is this straightforward ?

Please enlighten me if you can. :)

Marco vd Heijden
Moderator
5786
Marco vd Heijden  
if(ObjectFind(NULL,"Object_Name")<0)
  {
   // Object not found, create object(s).
  }
 

Return Value

If successful the function returns the number of the subwindow (0 means the main window of the chart), in which the object is found.

If the object is not found, the function returns a negative number.

Young Ho Seo
24011
Young Ho Seo  
Marco vd Heijden:

Return Value

If successful the function returns the number of the subwindow (0 means the main window of the chart), in which the object is found.

If the object is not found, the function returns a negative number.

I will try this. I will let you know if it can be successful.

Thanks for your help.

Kind regards.

Doerk Hilger
1497
Doerk Hilger  

What exactly is the problem here? I don´t get it. You cannot really destroy an object which was create with the library by using ObjectDelete. Either you use the classes or you use native MQL4. Maybe it disappears with ObjectDelete, but clean programming is something different. Please post the code and I can tell you what´s going wrong. The screenshot does not show me a CPanel instance, it shows a CDialog.

 

I get sucess when I call  "int OnInit()" function" for first time but when I repeat it couple of times then My panel become really ugrl looking with several other panels stacked on top of each other.". 

 One thing upfront: .Destroy() does not destroy the object panel, it destroys the content contained in the object panel. If the definition is like this:

CDialog panel;

panel.Create(....)

then the 

panel.Destroy()

should destroy the screen objects which have been created by the CDialog class. If you try this:

 

CDialog * panel = new CDialog;

panel.Create(...)

etc., and then later

delete panel;

everything should be gone, cause this will result in a call of the destruction in CWndContainer, which will result in a call of the .Delete() in CArrayObj and this will result in deletion of the contained instances which will result in destroying of all contained objects cause the free mode is used by default by CWndContainer. Got it? ;) But define the instance of the class outside OnInit(), cause otherwise, it will become a local instance and this will be deleted and destroyed when OnInit() ends. Same with normal definition of the panel, it must be defined as a static object variable outside the function. 

Doerk 

Alexander Puzanov
34705
Alexander Puzanov  
The problem is that CControlsDialog & CWndContainer crashes too often and all graphical objects still not deleted/destroyed

After re-start a new constructor do not use already existing graphic objects, but generates a new 5-digits prefix for its objects names

Only solution i've found - use special cleaning function, which from time to time search & kill all the wrecks

There is a lot of details in this solution - too hard to describe with my English. But i can show the most important part - search'n'kill function itself

void CControlsDialog::Clean_Objects(string s_Valid_Prefix) {
        int i_Object_ID = 0;
        string
                s_Name,
                s_Invlid_Prefix = "-----"
        ;
        while(StringLen(s_Invlid_Prefix) > 0) {
                ObjectsDeleteAll(0, s_Invlid_Prefix);
                s_Invlid_Prefix = "";
                i_Object_ID = ObjectsTotal();
                while(i_Object_ID-- > 0) {
                        s_Name = ObjectName(0, i_Object_ID);
                        if(StringFind(s_Name, "MinMax") != 5) continue;
                        s_Invlid_Prefix = StringSubstr(s_Name, 0, 5);
                        if(s_Valid_Prefix == s_Invlid_Prefix) s_Invlid_Prefix = "-----";
                        else break;
                }
        }
}

 

Doerk Hilger
1497
Doerk Hilger  
Alexander Puzanov:
The problem is that CControlsDialog & CWndContainer crashes too often and all graphical objects still not deleted/destroyed

After re-start a new constructor do not use already existing graphic objects, but generates a new 5-digits prefix for its objects names

Only solution i've found - use special cleaning function, which from time to time search & kill all the wrecks

There is a lot of details in this solution - too hard to describe with my English. But i can show the most important part - search'n'kill function itself

 

Totally wrong way.  The classes may be buggy, but crash? No. And if there is something with this classes which is really working good, then it is object and memory management. But due to lack of documentation, most people seem to misunderstand how to it all.

@Young - please post the snipped ...

Alexander Puzanov
34705
Alexander Puzanov  
Doerk:

Totally wrong way.

It works :)

Doerk:

The classes may be buggy, but crash? No

It crashes :)
123
To add comments, please log in or register