AppDialog with multiple Dialogs close problem

 

Hello everybody,

I'm trying to create a expert advisor (mql4) with multiple dialogs using standard library.

Foe convenience all the components inside dialog are prefixed with dialog name.

This is what I got so far, so there is one problem that I cannot figure it out:

  • when user click inside dialog1 or dialog2 automatically the dialog is closing (an exception to this rule is when the the edit input is clicked) 
    • so far I discovered that the visible (false) is triggered from ON_EVENT(ON_CLICK,m_button_close,OnClickButtonClose), but i dont click on close button so dig deeper and sow that the ON_EVENT macro must meet theese conditions 
    • id==(event+CHARTEVENT_CUSTOM) && lparam==control.Id()
      and i dont undestrand

  1. can click on header and dialog is not closing
  2. can drag the dialog and is not closing
  3. on close dialog header button dialog closes correctly


PS: i'm new at Mql4 / C++;

Dou you have any idea how to solve this?

Here is my code,

All the best.


//+------------------------------------------------------------------+
//|                                                 TestDialogV2.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict


#include <Controls/Dialog.mqh>
#include <Controls/Label.mqh>
#include <Controls/Edit.mqh>
#include <Controls/Button.mqh>
// dialog
CAppDialog mAppDialogMaster;
CDialog dialog1, dialog2;
CButton openDialog1Button,openDialog2Button;
CEdit dialog1Edit,dialog2Edit;
CLabel dialog1Label,dialog2Label;

int i_reason;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(i_reason != REASON_CHARTCHANGE)
     {
     // ########## START ########## - CREATE INITIAL APP DIALOG
      if(!mAppDialogMaster.Create(0,"Dialog Manager", 0, 10, 10, 10 + 210, 40 + 270))
        {
         return (INIT_FAILED);
        }

      if(!openDialog1Button.Create(0,"openDialog1", 0, 0, 0, 50, 50))
        {
         return (INIT_FAILED);
        }

      if(!openDialog1Button.Text("Open D1"))
        {
         return (INIT_FAILED);
        }

      if(!mAppDialogMaster.Add(openDialog1Button))
        {
         return (INIT_FAILED);
        }

      if(!openDialog2Button.Create(0,"openDialog2", 0, 60, 0, 110, 50))
        {
         return (INIT_FAILED);
        }

      if(!openDialog2Button.Text("Open D2"))
        {
         return (INIT_FAILED);
        }

      if(!mAppDialogMaster.Add(openDialog2Button))
        {
         return (INIT_FAILED);
        }

      if(!mAppDialogMaster.Run())
        {
         return (INIT_FAILED);
        }
        
     // ########## END ########## - CREATE INITIAL APP DIALOG


     // ########## START ########## - CREATE DIALOG 1
      if(!dialog1.Create(0,"dialog1", 0, 300, 10, 300 + 210, 40 + 270))
        {
         return (INIT_FAILED);
        }

      if(!dialog1.Visible(false))
        {
         return (INIT_FAILED);
        }
      if(!dialog1Edit.Create(0,"dialog1Edit", 0, 10, 10, 60, 30))
        {
         return (INIT_FAILED);
        }
      if(!dialog1.Add(dialog1Edit))
        {
         return (INIT_FAILED);
        }
      if(!dialog1Label.Create(0,"dialog1Label", 0, 60, 10,100, 30))
        {
         return (INIT_FAILED);
        }
      if(!dialog1Label.Text("D1"))
        {
         return (INIT_FAILED);
        }
      if(!dialog1.Add(dialog1Label))
        {
         return (INIT_FAILED);
        }

     // ########## END ########## - CREATE DIALOG 1

     // ########## START ########## - CREATE DIALOG 2
      if(!dialog2.Create(0,"dialog2", 0, 600, 10, 600 + 210, 40 + 270))
        {
         return (INIT_FAILED);
        }

      if(!dialog2.Visible(false))
        {
         return (INIT_FAILED);
        }

      if(!dialog2Edit.Create(0,"dialog2Edit", 0, 10, 10, 60, 30))
        {
         return (INIT_FAILED);
        }
      if(!dialog2.Add(dialog2Edit))
        {
         return (INIT_FAILED);
        }
      if(!dialog2Label.Create(0,"dialog2Label", 0, 60, 10,100, 30))
        {
         return (INIT_FAILED);
        }
      if(!dialog2Label.Text("D2"))
        {
         return (INIT_FAILED);
        }
      if(!dialog2Label.ColorBorder(clrRed))
        {
         return (INIT_FAILED);
        }
      if(!dialog2.Add(dialog2Label))
        {
         return (INIT_FAILED);
        }
     // ########## END ########## - CREATE DIALOG 2
      
     }

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   if(reason != REASON_CHARTCHANGE)
     {
      //EventKillTimer();
      mAppDialogMaster.Destroy(reason);
      dialog1.Destroy(reason);
      dialog2.Destroy(reason);
     }

   i_reason = reason;
  }

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if(id==CHARTEVENT_OBJECT_CLICK && sparam == "openDialog1")
     {

      dialog1.Visible(true);
      dialog1.Activate();
      return;
     }

   if(id==CHARTEVENT_OBJECT_CLICK && sparam == "openDialog2")
     {

      dialog2.Visible(true);
      dialog2.Activate();
      return;
     }

   if(StringFind(sparam,"dialog1") > -1)
     {
      dialog1.OnEvent(id, lparam, dparam, sparam);
      return ;
     }

   if(StringFind(sparam,"dialog2") > -1)
     {
      dialog2.OnEvent(id, lparam, dparam, sparam);
      return ;
     }

   mAppDialogMaster.OnEvent(id, lparam, dparam, sparam);
   dialog1.OnEvent(id, lparam, dparam, sparam);
   dialog2.OnEvent(id, lparam, dparam, sparam);
  }
//+------------------------------------------------------------------+ 

 

somebody any ideas?

 
any ideas what I'm doing wrong?
 
//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                                                       Lukas Roth |
//|                           https://www.mql5.com/en/users/lukiroth |
//+------------------------------------------------------------------+
#property copyright "Lukas Roth"
#property link      "https://www.mql5.com/en/users/lukiroth"
#property version   "1.00"
#property strict

#include <Controls/Dialog.mqh>
#include <Controls/Label.mqh>
#include <Controls/Button.mqh>

CAppDialog mAppDialogMaster;
CDialog dialog1, dialog2;
CButton openDialog1Button,openDialog2Button;
CEdit dialog1Edit,dialog2Edit;
CLabel dialog1Label,dialog2Label;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- AppDialog
   if(!mAppDialogMaster.Create(0,"Dialog Manager", 0, 10, 10, 10 + 210, 40 + 270))  { return(INIT_FAILED); }
   //--- Button 1
   if(!openDialog1Button.Create(0,"openDialog1", 0, 0, 0, 50, 50))                  { return(INIT_FAILED); }
   if(!openDialog1Button.Text("Open D1"))                                           { return(INIT_FAILED); }
   if(!mAppDialogMaster.Add(openDialog1Button))                                     { return(INIT_FAILED); } 
   //--- Button 2
   if(!openDialog2Button.Create(0,"openDialog2", 0, 60, 0, 110, 50))                { return(INIT_FAILED); }
   if(!openDialog2Button.Text("Open D2"))                                           { return(INIT_FAILED); }
   if(!mAppDialogMaster.Add(openDialog2Button))                                     { return(INIT_FAILED); } 
   
   //--- Dialog 1
   if(!dialog1.Create(0,"dialog1", 0, 300, 10, 300 + 210, 40 + 270))                { return(INIT_FAILED); } 
   if(!mAppDialogMaster.Add(dialog1))                                               { return(INIT_FAILED); }
   if(!dialog1.Hide())                                                              { return(INIT_FAILED); }
   //--- Edit
   if(!dialog1Edit.Create(0,"dialog1Edit", 0, 10, 10, 60, 30))                      { return(INIT_FAILED); }
   if(!dialog1.Add(dialog1Edit))                                                    { return(INIT_FAILED); }
   //--- Label
   if(!dialog1Label.Create(0,"dialog1Label", 0, 60, 10,100, 30))                    { return(INIT_FAILED); }
   if(!dialog1Label.Text("D1"))                                                     { return(INIT_FAILED); }
   if(!dialog1.Add(dialog1Label))                                                   { return(INIT_FAILED); }
   
   //--- Dialog 2
   if(!dialog2.Create(0,"dialog2", 0, 600, 10, 600 + 210, 40 + 270))                { return(INIT_FAILED); }
   if(!mAppDialogMaster.Add(dialog2))                                               { return(INIT_FAILED); } 
   if(!dialog2.Hide())                                                              { return(INIT_FAILED); }
   //--- Edit
   if(!dialog2Edit.Create(0,"dialog2Edit", 0, 10, 10, 60, 30))                      { return(INIT_FAILED); }
   if(!dialog2.Add(dialog2Edit))                                                    { return(INIT_FAILED); }
   //--- Label
   if(!dialog2Label.Create(0,"dialog2Label", 0, 60, 10,100, 30))                    { return(INIT_FAILED); }
   if(!dialog2Label.Text("D2"))                                                     { return(INIT_FAILED); }
   if(!dialog2Label.Color(clrRed))                                                  { return(INIT_FAILED); }
   if(!dialog2.Add(dialog2Label))                                                   { return(INIT_FAILED); }
   
   if(!mAppDialogMaster.Run())                                                      { return(INIT_FAILED); }
      
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   mAppDialogMaster.Destroy();
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{

}
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   mAppDialogMaster.OnEvent(id, lparam, dparam, sparam);

   //--- CUSTOM EVENTS
   if(id >= CHARTEVENT_CUSTOM)
   {    
        const int customID = id - CHARTEVENT_CUSTOM; //Get ID   
           
        //--- On end edit
        if(customID == ON_CLICK)
        {
                if(lparam == openDialog1Button.Id())
                {
                   if(dialog1.IsVisible()) { dialog1.Hide(); }
                   else                    { dialog1.Show(); }
                }
                else if(lparam == openDialog2Button.Id())
                {
                   if(dialog2.IsVisible()) { dialog2.Hide(); }
                   else                    { dialog2.Show(); }
                }
        }//End of ON_END_EDIT

        //--- On end edit
        else if(customID == ON_END_EDIT)
        {
                if(lparam == dialog1Edit.Id())
                {
                
                }
                
                else if(lparam == dialog1Edit.Id())
                {
                
                }
        }//End of ON_END_EDIT
   }
}


1. You need to add the dialog controls to the AppDialog

2. Use Run() function after all objects are created

3. U need to use the OnEvent fucntion so the appDialog precesses any events!

Just check in the code what I changed

 
Lukas Roth #:


1. You need to add the dialog controls to the AppDialog

2. Use Run() function after all objects are created

3. U need to use the OnEvent fucntion so the appDialog precesses any events!

Just check in the code what I changed


Thank you very much for your help.


EDIT: Works like a charm.