A task with buttons ! - page 7

 
Karputov Vladimir:
It's probably time to attach the test code. This will make it easier to experiment.

Here's

//+------------------------------------------------------------------+
//|                                                       Button.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   ButtonCreate(0,"A",0,20,50,100,20,CORNER_LEFT_UPPER,"Button A");
   ButtonCreate(0,"B",0,20,80,100,20,CORNER_LEFT_UPPER,"Button B");

   if((bool)ObjectGetInteger(0,"A",OBJPROP_STATE)==true)
      ObjectSetInteger(0,"B",OBJPROP_STATE,false);

   if((bool)ObjectGetInteger(0,"B",OBJPROP_STATE)==true)
      ObjectSetInteger(0,"A",OBJPROP_STATE,false);

  }
//+------------------------------------------------------------------+
bool ButtonCreate(const long              chart_ID=0,               // ID графика
                  const string            name="Button",            // имя кнопки
                  const int               sub_window=0,             // номер подокна
                  const int               x=0,                      // координата по оси X
                  const int               y=0,                      // координата по оси Y
                  const int               width=50,                 // ширина кнопки
                  const int               height=18,                // высота кнопки
                  const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // угол графика для привязки
                  const string            text="Button",            // текст
                  const string            font="Arial",             // шрифт
                  const int               font_size=10,             // размер шрифта
                  const color             clr=clrBlack,             // цвет текста
                  const color             back_clr=C'236,233,216',  // цвет фона
                  const color             border_clr=clrNONE,       // цвет границы
                  const bool              state=false,              // нажата/отжата
                  const bool              back=false,               // на заднем плане
                  const bool              selection=false,          // выделить для перемещений
                  const bool              hidden=true,              // скрыт в списке объектов
                  const long              z_order=0)                // приоритет на нажатие мышью
  {
//--- сбросим значение ошибки
   ResetLastError();
//--- создадим кнопку
   if(ObjectCreate(chart_ID,name,OBJ_BUTTON,sub_window,0,0))
     {

      //--- установим координаты кнопки
      ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
      ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
      //--- установим размер кнопки
      ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
      ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
      //--- установим угол графика, относительно которого будут определяться координаты точки
      ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
      //--- установим текст
      ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
      //--- установим шрифт текста
      ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
      //--- установим размер шрифта
      ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
      //--- установим цвет текста
      ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
      //--- установим цвет фона
      ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
      //--- установим цвет границы
      ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,border_clr);
      //--- отобразим на переднем (false) или заднем (true) плане
      ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
      //--- переведем кнопку в заданное состояние
      ObjectSetInteger(chart_ID,name,OBJPROP_STATE,state);
      //--- включим (true) или отключим (false) режим перемещения кнопки мышью
      ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
      ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
      //--- скроем (true) или отобразим (false) имя графического объекта в списке объектов
      ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
      //--- установим приоритет на получение события нажатия мыши на графике
      ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
     }
//--- успешное выполнение
   return(true);
  }
//+------------------------------------------------------------------+

Hopefully there will be solutions to the problem...

 
Vladimir Pastushak:

Where do I write the time? Do I create variables for each button to store the time in them?

Thank you all the problem solved, I found a simpler way, I myself sit and wonder ...

You found the solution on the second pagehttps://www.mql5.com/ru/forum/64408/page2#comment_1923439

and did not share.

Задача с кнопками !
Задача с кнопками !
  • www.mql5.com
Задача : При нажатии на любую из кнопок предыдущая нажатая кнопка должна отжаться. - Страница 2 - Категория: общее обсуждение
 
Event:

TC found a solution on the second page ofhttps://www.mql5.com/ru/forum/64408/page2#comment_1923439

and did not share

maybe someone will suggest a better one ...
 
Excuse me, but I'm always surprised by the code
bool b;

if(b==true)
   ...

I want to finish it:
bool b;

if(b==true)
  {
  ...
  }
else
  {
   if(b==false)
     ...
   else
     // не true и не false - что-то другое
  }
 
Ilyas:
Excuse me, but I'm always surprised by the code

I want to add to it:
This is a sample for tests, don't be picky...
 
Vladimir Pastushak:
Doesn't work in the tester...
Which terminal is it? In MT5 the timer works in the tester, you can use it to check. In MT4 the timer does not work either ((
 

You can make do with one static variable, as only one button is pressed.

The pressed button is memorised and then a comparison is made at each tick. If there is a difference, the button is released and the new value is stored.

 
And all the simple 2x2 solutions are gone, along with the advisers ...
 
bool stateA = false;
bool stateB = false;
bool stateC = false;

int init()
{
   ButtonCreate( 0, "A", 0, 20, 50, 100, 20, CORNER_LEFT_UPPER, "Button A" );
   ButtonCreate( 0, "B", 0, 20, 80, 100, 20, CORNER_LEFT_UPPER, "Button B" );
   ButtonCreate( 0, "C", 0, 20, 110, 100, 20, CORNER_LEFT_UPPER, "Button C" );
   
   return 0;
}

void OnTick()
{
   if( stateA != (bool)ObjectGetInteger( 0, "A", OBJPROP_STATE ) )
   {
      stateA = true;
      stateB = false;
      stateC = false;
      ObjectSetInteger( 0, "B", OBJPROP_STATE, false );
      ObjectSetInteger( 0, "C", OBJPROP_STATE, false );
   }
   
   if( stateB != (bool)ObjectGetInteger( 0, "B", OBJPROP_STATE ) )
   {
      stateA = false;
      stateB = true;
      stateC = false;
      ObjectSetInteger( 0, "A", OBJPROP_STATE, false );
      ObjectSetInteger( 0, "C", OBJPROP_STATE, false );
   }
   
   if( stateC != (bool)ObjectGetInteger( 0, "C", OBJPROP_STATE ) )
   {
      stateA = false;
      stateB = false;
      stateC = true;
      ObjectSetInteger( 0, "A", OBJPROP_STATE, false );
      ObjectSetInteger( 0, "B", OBJPROP_STATE, false );
   }
}
 
static int t=0;

void OnTick()
{
   ButtonCreate( 0, "A", 0, 20, 50, 100, 20, CORNER_LEFT_UPPER, "Button A" );
   ButtonCreate( 0, "B", 0, 20, 80, 100, 20, CORNER_LEFT_UPPER, "Button B" );
   ButtonCreate( 0, "C", 0, 20, 110, 100, 20, CORNER_LEFT_UPPER, "Button C" );
   
   if( (bool)ObjectGetInteger( 0, "A", OBJPROP_STATE ) && t!=0)
   {
      t=0;
      ObjectSetInteger( 0, "B", OBJPROP_STATE, false );
      ObjectSetInteger( 0, "C", OBJPROP_STATE, false );
   }
   
   if( stateB != (bool)ObjectGetInteger( 0, "B", OBJPROP_STATE ) && t!=1 )
   {
      t=1;
      ObjectSetInteger( 0, "A", OBJPROP_STATE, false );
      ObjectSetInteger( 0, "C", OBJPROP_STATE, false );
   }
   
   if( stateC != (bool)ObjectGetInteger( 0, "C", OBJPROP_STATE ) && t!=2 )
   {
      t=2;
      ObjectSetInteger( 0, "A", OBJPROP_STATE, false );
      ObjectSetInteger( 0, "B", OBJPROP_STATE, false );
   }
}