RadioButton Group control

 

I am creating a radio button group in a separate indicator window on my charts but having difficulty with some of the settings. My code so far:

#property indicator_chart_window
#include <stderror.mqh>
#include <stdlib.mqh>

#include <Controls\Dialog.mqh>
#include <Controls\RadioButton.mqh>
#include <Controls\RadioGroup.mqh>

CRadioGroup          m_radiogroup;    

int OnInit()
{
   int subwin = WindowFind("MyIndicator");
   m_radiogroup.Create(0,"test",subwin,20,20,200,100);
   m_radiogroup.ColorBackground(clrGray);
   m_radiogroup.ColorBorder(clrGray);
   
   for(int i=0;i<4;i++)
   {
   switch(i)
   {
    case 0: string name = "Button 1";break;
    case 1: name = "Button 2";break;
    case 2: name = "Button 3";break;
    case 3: name = "Button 4";
   }
   if(!m_radiogroup.AddItem(name,1<<i))
        return(false);  
   }  
    
   //select the first button
   m_radiogroup.Value(1<<0);

  return(INIT_SUCCEEDED);
  }

This does display a radiobutton group but I am having difficulty in making the whole thing look nice i.e same colour, different font colour/size etc;

I cannot find the right commands to do this - is anybody able to help please?


Also I noticed that when a new radiobutton is selected the old button is still selected. This did not happen when I placed the radiobutton group on a panel in previous testing where there was only one button allowed to be selected at any one time. I thought the RadioGroup include file would have contained this code but obviously not. 

thanks for any help.

 

It's interesting I've had no comments or feedback on this subject! Normally by now I would have been told I am an idiot and to go and read the documentation.

It seems to me these new graphic controls are way too complicated especially for a non-programmer like me! I also thought from the very limited amount of information and articles written so far that the 'include' files contained all the necessary code to operate the controls correctly.

I have managed to create something which is nearly what I am looking for but I am not convinced I am using the correct commands etc; 

I am creating a standalone radiobutton group control in a subwindow on my chart.

#property indicator_chart_window
#include <stderror.mqh>
#include <stdlib.mqh>

#include <Controls\Dialog.mqh>
#include <Controls\RadioButton.mqh>
#include <Controls\RadioGroup.mqh>

CRadioGroup          m_radiogroup;    

int OnInit()
{
   int subwin = WindowFind("MyIndicator");
   m_radiogroup.Create(0,"test",subwin,20,20,200,100);
   m_radiogroup.ColorBackground(clrBlue);
   m_radiogroup.ColorBorder(clrBlue);
   
   for(int i=0;i<4;i++)
   {
   switch(i)
   {
    case 0: string name = "Button 1";break;
    case 1: name = "Button 2";break;
    case 2: name = "Button 3";break;
    case 3: name = "Button 4";
   }
   if(!m_radiogroup.AddItem(name,i))
        return(false);  
   }  
    ObjectSetInteger(0,"testItem"+i+"Label",OBJPROP_COLOR,clrYellow);
    ObjectSetInteger(0,"testItem"+i+"Label",OBJPROP_BGCOLOR,clrBlue);
    ObjectSetInteger(0,"testItem"+i+"Label",OBJPROP_BORDER_COLOR,clrBlue);
    ObjectSetString(0,"testItem"+i+"Label",OBJPROP_FONT,"Arial Italic");

   //select the first button
   m_radiogroup.Value(0);

  return(INIT_SUCCEEDED);
  }

So using the good old tried and trusted code from MT4 to change background colour, font etc;  I get this:


However, you can see that it's all a bit messy having to use the system generated label names associated to the radiobuttons.

Also, normal radiobutton selection is not working i.e only one button selected at a time. I thought this might have been invoked through the RadioGroup.mqh file with this included code:

//+------------------------------------------------------------------+
//| Sett current item                                                |
//+------------------------------------------------------------------+
void CRadioGroup::Select(const int index)
  {
//--- disable the "ON" state
   if(m_current!=-1)
      RowState(m_current-m_offset,false);
//--- enable the "ON" state
   if(index!=-1)
      RowState(index-m_offset,true);
//--- save value
   m_current=index;
  }
//+------------------------------------------------------------------+
//| Redraw                                                           |
//+------------------------------------------------------------------+
bool CRadioGroup::Redraw(void)
  {
//--- loop by "rows"
   for(int i=0;i<m_total_view;i++)
     {
      //--- copy text
      if(!m_rows[i].Text(m_strings.At(i+m_offset)))
         return(false);
      //--- select
      if(!RowState(i,(m_current==i+m_offset)))
         return(false);
     }
//--- succeed
   return(true);
  }
//+------------------------------------------------------------------+
//| Change state                                                     |
//+------------------------------------------------------------------+
bool CRadioGroup::RowState(const int index,const bool select)
  {
//--- check index
   if(index<0 || index>=ArraySize(m_rows))
      return(true);
//--- change state
   return(m_rows[index].State(select));
  } 

Any pointers would be appreciated.

thanks.

 

This post has given me a deeper understanding of how these Controls work. You inspired me to make a series of items I call BetterControls. Here is the code I made for BCRadioGroup. I'm going to take this knowledge and make it simple to center text on a label too lol.

//+------------------------------------------------------------------+
//| Includes                                                         |
//+------------------------------------------------------------------+
#include <Controls\RadioGroup.mqh>

//+------------------------------------------------------------------+
//| BCRadioGroup                                                     |
//+------------------------------------------------------------------+
class BCRadioGroup : public CRadioGroup {
   private:
      int m_i_ItemCount;
   public:
      BCRadioGroup();
      void LabelColorBackground(color);
      void LabelColorBorder(color);
      void LabelColor(color);
      bool AddItem(const string name,const long value=0);
      
      // getters and setters
      void ItemCount(int val);
      int ItemCount();
};

//+------------------------------------------------------------------+
//| Getters and setters                                              |
//+------------------------------------------------------------------+
void BCRadioGroup::ItemCount(int val) {m_i_ItemCount=val;}
int BCRadioGroup::ItemCount(void) {return m_i_ItemCount;}

//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
BCRadioGroup::BCRadioGroup(void) {
   m_i_ItemCount = 0;
}

//+------------------------------------------------------------------+
//| Set label background color                                       |
//+------------------------------------------------------------------+
void BCRadioGroup::LabelColorBackground(color c) {
   for(int i=0;i<ItemCount();i++) {
      Print(Name()+IntegerToString(i)+"Label");
      ObjectSetInteger(0,Name()+"Item"+IntegerToString(i)+"Label",OBJPROP_BGCOLOR,c);
   }
}

//+------------------------------------------------------------------+
//| Set label border color                                           |
//+------------------------------------------------------------------+
void BCRadioGroup::LabelColorBorder(color c) {
   for(int i=0;i<ItemCount();i++) {
      ObjectSetInteger(0,Name()+"Item"+IntegerToString(i)+"Label",OBJPROP_BORDER_COLOR,c);
   }
}

//+------------------------------------------------------------------+
//| Set label text color                                             |
//+------------------------------------------------------------------+
void BCRadioGroup::LabelColor(color c) {
   for(int i=0;i<ItemCount();i++) {
      ObjectSetInteger(0,Name()+"Item"+IntegerToString(i)+"Label",OBJPROP_COLOR,c);
   }
}

//+------------------------------------------------------------------+
//| Add item and increment count                                     |
//+------------------------------------------------------------------+
bool BCRadioGroup::AddItem(const string name,const long value=0) {
   CRadioGroup::AddItem(name,value);
   m_i_ItemCount++;
   return true;
}