Custom Indicator based on CAppDialog not displaying when added to template

 

I have the following simple custom indicator based on the class CAppDialog.

It creates a instance of MyAppDialog derived from CAppDialog.

MyAppDialog in turn creates a instance of MyWndContainer derived from CWndContainer.

MyWndContainer then creates an instance of a CLabel.

If I now add this indicator to a chart and save it as a template and try to load the template on a different chart, it displays the subwindow but the dialog does not get displayed.

If I comment out the code creating the CLabel on the MyWndContainer and repeat the template process it does get displayed.


Just for testing the problem I created a CLabel on the MyAppDialog and then the error does not occur.


Included is the code :

For the Indicator:

//+------------------------------------------------------------------+
//|                                                       MyTest.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_plots               0
#property indicator_buffers             0
#property indicator_minimum             0.0
#property indicator_maximum             0.0
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

#include <MyAppDialog.mqh>

MyAppDialog InstMyAppDialog;

int OnInit()
  {
  if(!InstMyAppDialog.Create(0,"My App Dialog",0,0,0,0,300))
     return(INIT_FAILED);
  if(!InstMyAppDialog.Run())
     return(INIT_FAILED);   
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   InstMyAppDialog.ChartEvent(id,lparam,dparam,sparam);
  }
//+------------------------------------------------------------------+

For the MyAppDialog class:

//+------------------------------------------------------------------+
//|                                                  MyAppDialog.mqh |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#include <Controls\Dialog.mqh>
#include <MyWndContainer.mqh>

class MyAppDialog : public CAppDialog
  {
private:
   MyWndContainer    InstMyWndContainer;
   CLabel            lblMyAppLabel;
   bool              CreateMyContainer(void);
   bool              CreateMyAppLabel(void);

public:
                     MyAppDialog();
                    ~MyAppDialog();

virtual bool      Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);                    
                    
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
MyAppDialog::MyAppDialog()
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
MyAppDialog::~MyAppDialog()
  {
  }
//+------------------------------------------------------------------+

bool MyAppDialog::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
  {
//--- calling the method of the parent class
   if(!CAppDialog::Create(chart,name,subwin,x1,y1,x2,y2))  
      return(false);
//--- additional controls shall be created here
   if(!CreateMyContainer())
      return(false);
   if(!CreateMyAppLabel())
      return(false);   
   Run();
//--- success
   return(true);
  }
  
bool MyAppDialog::CreateMyContainer(void)
  {  
  if(!InstMyWndContainer.Create(m_chart_id,"My Container",m_subwin,0,0,0,300))
      return(false);
  if(!Add(InstMyWndContainer))
      return(false);  
  InstMyWndContainer.Show();       
  return(true);    
  }
  
bool MyAppDialog::CreateMyAppLabel(void)    
   {
   if(!lblMyAppLabel.Create(m_chart_id,"lblMyAppLabel"+m_name, m_subwin,100,5,50,102))              
      return(false);
  lblMyAppLabel.Text("MyAppLabel:");
  lblMyAppLabel.FontSize(8);
  if(!Add(lblMyAppLabel))
      return(false);
   return(true);
   }

For the MyWndContainer class:

//+------------------------------------------------------------------+
//|                                               MyWndContainer.mqh |
//|                                                                  |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright ""
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#include <Controls\WndContainer.mqh>
#include <Controls\Label.mqh>

class MyWndContainer : public CWndContainer
  {
private:
   CLabel      lblMyLabel;
   bool        CreateMyLabel(void);

public:
                     MyWndContainer();
                    ~MyWndContainer();
                    
virtual bool      Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);  
                    
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
MyWndContainer::MyWndContainer()
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
MyWndContainer::~MyWndContainer()
  {
  }
//+------------------------------------------------------------------+

bool MyWndContainer::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
   {
   if(!CWndContainer::Create(chart,name,subwin,x1,y1,x2,y2))  
      return(false);
   if(!CreateMyLabel())
      return(false);
   return(true);
   }      


bool MyWndContainer::CreateMyLabel(void)
  {
  if(!lblMyLabel.Create(m_chart_id,"lblMyLabel"+m_name, m_subwin,10,5,50,22))              
      return(false);
  lblMyLabel.Text("MyLabel:");
  lblMyLabel.FontSize(8);
  if(!Add(lblMyLabel))
      return(false);   
  return(true);    
  }



If I comment out the code creating the CLabel in the CWndContainer

bool MyWndContainer::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
   {
   if(!CWndContainer::Create(chart,name,subwin,x1,y1,x2,y2))  
      return(false);
//   if(!CreateMyLabel())
//      return(false);
   return(true);
   } 

I don't get the error when loading the indicator from a template in MetaTrader.


Any idea if there is something wrong in my coding or how to get around this problem?

 
Anybody Please!
 
Raaneip:
Anybody Please!

It's because when you apply the template, you already have a label with a given name and your indicator which is also applied with the template, try to recreate an object with the same name.

  if(!lblMyLabel.Create(m_chart_id,"lblMyLabel"+m_name, m_subwin,10,5,50,22)) 
 
angevoyageur:

It's because when you apply the template, you already have a label with a given name and your indicator which is also applied with the template, try to recreate an object with the same name.




Thank you very much angevoyageur.


So I edited the .tpl file and removed all the definitions of the objects in it but left the "definition" of the "indicator" in it and voila it gets displayed.

Reason: