How to create a ComboBox object in an indicator subwindow?

 

Dear all,

  I created a  ComboBox object in an indicator subwindow(by the code attached), But when I click the drop-down arrow next to the control, nothing was pop out. What is wrong with my code? Many thanks for your time and efforts.

//+------------------------------------------------------------------+
//|                                                test_combobox.mq5 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#include <Controls\ComboBox.mqh>
#property indicator_separate_window
#property indicator_plots 0
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
CComboBox comboBox1;

int OnInit()
{
  if(CreateComboBox()) return(INIT_SUCCEEDED);
  else
  { 
    string msg;
    msg=StringFormat("error in CreateComboBox(), #",GetLastError());
    Alert(msg);
    return(INIT_FAILED);
  }
}
  
bool CreateComboBox()
{
  long subWindowID=ChartWindowFind();
  int x1=100, y1=100, xsize=150, ysize=20;
  if(!comboBox1.Create(0,"comboBox1",(int)subWindowID,x1,y1,x1+xsize,y1+ysize)) return(false); // create the ComboBox object.

  // add items to the ComboBox object.
  if(!comboBox1.AddItem("0, PL",0)) return(false);
  if(!comboBox1.AddItem("0, TL",0)) return(false);
  if(!comboBox1.AddItem("0, MA",0)) return(false);
  if(!comboBox1.AddItem("0, RSI",0)) return(false);

  return(true);
}

void OnDeinit(const int reason)
{
  Alert("OnDeinit invoked, delete the combobox1 object.");
  comboBox1.Destroy(reason);
} 


//+------------------------------------------------------------------+
//| 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);
  }
 

This is not to answer your problem, but it may interesting you.

https://www.mql5.com/en/articles/345 

Create Your Own Graphical Panels in MQL5
Create Your Own Graphical Panels in MQL5
  • 2012.02.07
  • MetaQuotes Software Corp.
  • www.mql5.com
The MQL5 program usability is determined by both its rich functionality and an elaborate graphical user interface. Visual perception is sometimes more important than fast and stable operation. Here is a step-by-step guide to creating display panels on the basis of the Standard Library classes on your own.
 

Actually I dont know how it works, but I edited the examples EA in MT5 named "Controls" which is in folder "Experts\Examples\Controls". I edited (deleted some lines) so we can study more about combo box. There are 2 files, one as indicator and the other as include files. Both should be put in indicator folder. The first is indicator named "Controls1.mq5" and the second named "ControlsDialog1.mqh".

 

"Controls1.mq5" 

#include "ControlsDialog1.mqh"
//+------------------------------------------------------------------+
//| Global Variables                                                 |
//+------------------------------------------------------------------+
CControlsDialog ExtDialog;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create application dialog
   if(!ExtDialog.Create(0,"Controls",0,20,20,360,124))
     return(INIT_FAILED);
//--- run application
   ExtDialog.Run();
//--- succeed
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy dialog
   ExtDialog.Destroy(reason);
  }
//+------------------------------------------------------------------+
//| Expert chart event 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(rates_total);
  }

void OnChartEvent(const int id,         // event ID  
                  const long& lparam,   // event parameter of the long type
                  const double& dparam, // event parameter of the double type
                  const string& sparam) // event parameter of the string type
  {
   ExtDialog.ChartEvent(id,lparam,dparam,sparam);
  }
//+------------------------------------------------------------------+

 

"ControlsDialog1.mqh" 

//+------------------------------------------------------------------+
//|                                              ControlsDialog1.mqh |
//|                   Copyright 2009-2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#include <Controls\Dialog.mqh>
#include <Controls\ComboBox.mqh>
//+------------------------------------------------------------------+
//| defines                                                          |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Class CControlsDialog                                            |
//| Usage: main dialog of the Controls application                   |
//+------------------------------------------------------------------+
class CControlsDialog : public CAppDialog
  {
private:
//   CEdit             m_edit;                          // the display field object
     CComboBox         m_combo_box;                     // the dropdown list object

public:
                     CControlsDialog(void);
                    ~CControlsDialog(void);
   //--- create
   virtual bool      Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);
   //--- chart event handler
//   virtual bool      OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);

protected:
   //--- create dependent controls
//   bool              CreateEdit(void);
   bool              CreateComboBox(void);
   //--- handlers of the dependent controls events
   void              OnChangeComboBox(void);
  };
//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CControlsDialog::CControlsDialog(void)
  {
  }
//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
CControlsDialog::~CControlsDialog(void)
  {
  }
//+------------------------------------------------------------------+
//| Create                                                           |
//+------------------------------------------------------------------+
bool CControlsDialog::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
  {
   if(!CAppDialog::Create(chart,name,subwin,x1,y1,x2,y2))
      return(false);
//--- create dependent controls
//   if(!CreateEdit())
//      return(false);
   if(!CreateComboBox())
      return(false);
//--- succeed
   return(true);
  }
//+------------------------------------------------------------------+
//| Create the "ComboBox" element                                    |
//+------------------------------------------------------------------+
bool CControlsDialog::CreateComboBox(void)
  {
//--- coordinates
   int x1=11;
   int y1=11;
   int x2=x1+150;
   int y2=y1+20;
//--- create
   if(!m_combo_box.Create(m_chart_id,m_name+"ComboBox",m_subwin,x1,y1,x2,y2))
      return(false);
   if(!Add(m_combo_box))
      return(false);
//--- fill out with strings
   for(int i=0;i<16;i++)
      if(!m_combo_box.ItemAdd("Item "+IntegerToString(i)))
         return(false);
//--- succeed
   return(true);
  }

 Hope it help.

Files:
 
taozemin:

Dear all,

  I created a  ComboBox object in an indicator subwindow(by the code attached), But when I click the drop-down arrow next to the control, nothing was pop out. What is wrong with my code? Many thanks for your time and efforts.

Dear Belido,

  Thank you for your effective information! I followed the link In your reply and add the CreateComboBox() function as a member function of the CPanelDialog Class. And the ComboBox that I needed is created there. To make it clear, I have attached the source code in case someone might need it....

bool CPanelDialog::CreateComboBox(void)
{
   int x1=100, y1=100, xsize=150, ysize=20;
   int x2=x1+xsize;
   int y2=y1+ysize;
   long subWinID=ChartWindowFind();

   if(!m_comboBox.Create(0,"ComboBox",(int)subWinID,x1,y1,x2,y2))
      return(false);
      
     if(!m_comboBox.AddItem("PL: OPPPPPPPPPPPP",0)) return(false);
     if(!m_comboBox.AddItem("TL: Trend Line",1)) return(false);
     if(!m_comboBox.AddItem("MA: Moving Average",2)) return(false);
     if(!m_comboBox.AddItem("RSI: RSI Indicator",3)) return(false);
     if(!Add(m_comboBox)) return(false);
   /* ATTENTION: the above call to CPanelDialog::Add(control) is definietely necessary.
      otherwise nothing will be popped out when the arrow of the combobox is clicked. */
//--- succeed
   Alert("the combobox is created ok!");
   return(true);
}
Files:
PanelDialog.mqh  14 kb
 

Combo

 

Hello,

 

i used this code in my EA, but i dont understand fully, why i have not the combobox only and also the panel around

can you explain?

 

amando 

Reason: