title: Mql4 How to set toggle button ON OFF state?
body: <p>I'm making a line of on-chart buttons that will launch specific functions and indicators. So far so good the buttons load an indicator when pressed and delete it when the button is unpressed.</p>
  <p>But I would like to add two more functions to the button but I'm doing something wrong and it doesn't work properly and I need some help.</p>
  <p>1st - I would like the button do be able to detect indicator on chart an automatically set the button to &quot;ON&quot; state in case I will drag and drop the indicator from indicator list.</p>
  <p>2nd - when I press the button it will launch an indicator popup window and we have 2 options;</p>
  <p>1 - &quot;OK&quot; which will launch the indicator on the chart and the button will stay in &quot;ON&quot; state.</p>
  <p>2 - &quot;CANCEL&quot; which will abort lunching the indicator but it will leave the button in &quot;ON&quot; state. I would like the button to go back to the &quot;OFF&quot; state just after I press &quot;CANCEL&quot;.</p>
  <p>Here is a peace of the code for testing.</p>
  <pre><code>#property indicator_chart_window
  string sButtonName1 =&quot;B1&quot;;
  string sButtonName2 =&quot;B2&quot;;
  const string IndiName1  =&quot;X&quot;;
  const string IndiName2  =&quot;Y&quot;;
  
  //--------------------------------------------------------------
  
  color UnloadedColor=clrGray;
  color LoadedColor=clrBlue;
  color currentColor;
  
  //--------------------------------------------------------------
  
  #import &quot;user32.dll&quot;
  void keybd_event(int bVk,int bScan,int dwFlags,int dwExtraInfo);
  #define  VK_L 0x4C
  #define  VK_M 0x4D
  #define  VK_MENU 0x12
  #define  KEYEVENTF_KEYUP 0x0002
  #import
  
  //+------------------------------------------------------------------+
  int OnInit()
    {
     int buttonwindow=ObjectFind(sButtonName1);
     int buttonwindow1=ObjectFind(sButtonName2);
   {
    if(buttonwindow&lt;0)
       CreateButton1(sButtonName1,UnloadedColor,&quot;Arial&quot;,0);
    if(buttonwindow1&lt;0)
       CreateButton2(sButtonName2,UnloadedColor,&quot;Arial&quot;,0);
   }
     return(INIT_SUCCEEDED);
    }
  //+----------------------------------------------------------------------------------+
  //|------------------------CreateButton 1--------------------------------------------|
  //+----------------------------------------------------------------------------------+
  void CreateButton1(string sName1,color sColor,string sFont=&quot;Arial&quot;,string sText=&quot;&quot;)
    {
     ObjectCreate(0,sName1,OBJ_BUTTON,0,0,0);
     ObjectSetInteger(0,sName1,OBJPROP_XDISTANCE,725);
     ObjectSetInteger(0,sName1,OBJPROP_YDISTANCE,0);
     ObjectSetInteger(0,sName1,OBJPROP_XSIZE,50);
     ObjectSetInteger(0,sName1,OBJPROP_YSIZE,50);
     ObjectSetInteger(0,sName1,OBJPROP_CORNER,4);
     ObjectSetString(0,sName1,OBJPROP_TEXT,&quot;X&quot;);
     ObjectSetInteger(0,sName1,OBJPROP_COLOR,sColor);
     ObjectSetString(0,sName1,OBJPROP_FONT,sFont);
     ObjectSetInteger(0,sName1,OBJPROP_FONTSIZE,26);
     ObjectSetInteger(0,sName1,OBJPROP_STATE,false);
    }
  //+----------------------------------------------------------------------------------+
  //|------------------------CreateButton 2--------------------------------------------|
  //+----------------------------------------------------------------------------------+
  void CreateButton2(string sName2,color sColor,string sFont=&quot;Arial&quot;,string sText=&quot;&quot;)
    {
     ObjectCreate(0,sName2,OBJ_BUTTON,0,0,0);
     ObjectSetInteger(0,sName2,OBJPROP_XDISTANCE,775);
     ObjectSetInteger(0,sName2,OBJPROP_YDISTANCE,0);
     ObjectSetInteger(0,sName2,OBJPROP_XSIZE,50);
     ObjectSetInteger(0,sName2,OBJPROP_YSIZE,50);
     ObjectSetInteger(0,sName2,OBJPROP_CORNER,4);
     ObjectSetString(0,sName2,OBJPROP_TEXT,&quot;Y&quot;);
     ObjectSetInteger(0,sName2,OBJPROP_COLOR,sColor);
     ObjectSetString(0,sName2,OBJPROP_FONT,sFont);
     ObjectSetInteger(0,sName2,OBJPROP_FONTSIZE,26);
     ObjectSetInteger(0,sName2,OBJPROP_STATE,false);
    }
  //+------------------------------------------------------------------+
  //|                                                                  |
  //+------------------------------------------------------------------+
  void OnChartEvent(const int id,
                const long &amp;lparam,
                const double &amp;dparam,
                const string &amp;sparam) 
   {
  //+----------------------------------------------------------------------------------+
  //+--------------------------------------- BUTTON 1 ---------------------------------+
  //+----------------------------------------------------------------------------------+
   {
      {
       if(id==CHARTEVENT_OBJECT_CLICK &amp;&amp; sparam==sButtonName1)
         {
          currentColor=ObjectGetInteger(0,sButtonName1,OBJPROP_COLOR);
          if(currentColor==UnloadedColor)
            {
             keybd_event(VK_MENU,0,0,0);
             keybd_event(VK_L,0,0,0);
             keybd_event(VK_L,0,KEYEVENTF_KEYUP,0);
             keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);
  
             ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,LoadedColor);
             return;
            }
          else
             if(currentColor==LoadedColor)
               {
                ChartIndicatorDelete(0,0,IndiName1);
                ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,UnloadedColor);
               }
         }
      }
  
    //+----------------------------------------------------------------------------------+
    //+---------------------------------------BUTTON 2-----------------------------------+
    //+----------------------------------------------------------------------------------+
  
      {
       if(id==CHARTEVENT_OBJECT_CLICK &amp;&amp; sparam==sButtonName2)
         {
          currentColor=ObjectGetInteger(0,sButtonName2,OBJPROP_COLOR);
          if(currentColor==UnloadedColor)
            {
             keybd_event(VK_MENU,0,0,0);
             keybd_event(VK_M,0,0,0);
             keybd_event(VK_M,0,KEYEVENTF_KEYUP,0);
             keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);
  
             ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,LoadedColor);
             return;
            }
          else
             if(currentColor==LoadedColor)
               {
                ChartIndicatorDelete(0,0,IndiName2);
                ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,UnloadedColor);
               }
         }
      }
   }
  
  /*
  
  //+-------------------------------------NOT WORKING-----------------------------------+
  
  //------------------------------ DETECT INDICATOR ON THE CHART-------------------------------
  
   {
    int indicators_total = ChartIndicatorsTotal(0,0);
    for(int i = 0; indicators_total &gt; i; i++)
  
  //+------------------------- INDICATOR 1 DETECTED -----------------------------------
  
      {
       if(ChartIndicatorName(0,0,i)==IndiName1)
         {
          currentColor=ObjectGetInteger(0,sButtonName1,OBJPROP_COLOR);
          if(currentColor==UnloadedColor)
            {
             ObjectSetInteger(0,sButtonName1,OBJPROP_STATE,true);
             ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,LoadedColor);
            }
         }
  //+------------------------- INDICATOR 1 NOT DETECTED -----------------------------------
  
       if(!(ChartIndicatorName(0,0,i)==IndiName1))
         {
          currentColor=ObjectGetInteger(0,sButtonName1,OBJPROP_COLOR);
          if(currentColor==LoadedColor)
            {
             ObjectSetInteger(0,sButtonName1,OBJPROP_STATE,false);
             ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,UnloadedColor);
            }
         }
  //+------------------------- INDICATOR 2 DETECTED-----------------------------------
       if(ChartIndicatorName(0,0,i)==IndiName2)
         {
          currentColor=ObjectGetInteger(0,sButtonName2,OBJPROP_COLOR);
          if(currentColor==UnloadedColor)
            {
             ObjectSetInteger(0,sButtonName2,OBJPROP_STATE,true);
             ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,LoadedColor);
            }
         }
  //+------------------------- INDICATOR 2 NOT DETECTED -----------------------------------
  
       if(!(ChartIndicatorName(0,0,i)==IndiName2))
         {
          currentColor=ObjectGetInteger(0,sButtonName2,OBJPROP_COLOR);
          if(currentColor==LoadedColor)
            {
             ObjectSetInteger(0,sButtonName2,OBJPROP_STATE,false);
             ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,UnloadedColor);
            }
         }
      }
   }
  //+-------------------------------------------------------------------------------------
  
  */
    }
  //+------------------------------------------------------------------+
  int OnCalculate(const int rates_total,
              const int prev_calculated,
              const datetime &amp;time[],
              const double &amp;open[],
              const double &amp;high[],
              const double &amp;low[],
              const double &amp;close[],
              const long &amp;tick_volume[],
              const long &amp;volume[],
              const int &amp;spread[])
    {
     return(rates_total);
    }
  //+------------------------------------------------------------------+
  </code></pre>