Change text in a CLabel
febrero59:
I keep building my panel from scratch and, never have, I'm stumbling on pretty much everything.
I am starting with Class CControlsDialog. I've done well with buttons, labels, and edit boxes.
But I have a problem with https://www.mql5.com/en/docs/standardlibrary/controls/clabel.
I need to change the label text, but I can't get the syntax right and I can't find a suitable example on the net.
Can you give me a link or explanation about it?
Thank you very much.
it is quite clear in the documentation but post your code attempt with the code button </> if you need some help
Documentation on MQL5: Standard Library / Panels and Dialogs / CLabel
- www.mql5.com
CLabel - Panels and Dialogs - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
#include <Controls\Dialog.mqh> #include <Controls\Label.mqh> //+------------------------------------------------------------------+ //| defines | //+------------------------------------------------------------------+ //--- indents and gaps #define INDENT_LEFT (11) // indent from left (with allowance for border width) #define INDENT_TOP (11) // indent from top (with allowance for border width) #define INDENT_RIGHT (11) // indent from right (with allowance for border width) #define INDENT_BOTTOM (11) // indent from bottom (with allowance for border width) #define CONTROLS_GAP_X (5) // gap by X coordinate #define CONTROLS_GAP_Y (5) // gap by Y coordinate //--- for buttons #define BUTTON_WIDTH (100) // size by X coordinate #define BUTTON_HEIGHT (20) // size by Y coordinate //--- for the indication area #define EDIT_HEIGHT (20) // size by Y coordinate //--- for group controls #define GROUP_WIDTH (150) // size by X coordinate #define LIST_HEIGHT (179) // size by Y coordinate #define RADIO_HEIGHT (56) // size by Y coordinate #define CHECK_HEIGHT (93) // size by Y coordinate //+------------------------------------------------------------------+ //| Class CControlsDialog | //| Usage: main dialog of the Controls application | //+------------------------------------------------------------------+ class CControlsDialog : public CAppDialog { private: CLabel m_label; // CLabel 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 CreateLabel(void); //--- handlers of the dependent controls events void OnClickLabel(void); }; //+------------------------------------------------------------------+ //| Event Handling | //+------------------------------------------------------------------+ EVENT_MAP_BEGIN(CControlsDialog) EVENT_MAP_END(CAppDialog) //+------------------------------------------------------------------+ //| 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(!CreateLabel()) return(false); //--- succeed return(true); } //+------------------------------------------------------------------+ //| Create the "CLabel" | //+------------------------------------------------------------------+ bool CControlsDialog::CreateLabel(void) { //--- coordinates int x1=INDENT_RIGHT; int y1=INDENT_TOP+CONTROLS_GAP_Y; int x2=x1+100; int y2=y1+20; //--- create if(!m_label.Create(m_chart_id,m_name+"Label",m_subwin,x1,y1,x2,y2)) return(false); if(!m_label.Text("Label123456")) return(false); if(!Add(m_label)) return(false); Comment(m_label.Text()); //--- succeed return(true); } //+------------------------------------------------------------------+ //| Global Variables | //+------------------------------------------------------------------+ CControlsDialog ExtDialog; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- create application dialog if(!ExtDialog.Create(0,"Controls",0,40,40,380,344)) return(INIT_FAILED); //--- run application ExtDialog.Run(); //--- succeed return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- Comment(""); //--- destroy dialog ExtDialog.Destroy(reason); } //+------------------------------------------------------------------+ //| Expert chart event function | //+------------------------------------------------------------------+ 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); // Comment(label, "-", dparam); } void OnTick() { double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); // ObjectSetString(0,label,OBJPROP_TEXT, (string)Ask); // Label.TextChange(0,label,(string)Ask)); label.SetText(Ask); }
It should be very easy, but I don't know how to pass the Ask price to the label...
Use
label.Text(DoubleToString(Ask,Digits()));
Excuse me Vladimir, what do I have to do so that I don't get the error: " 'label' - undeclared identifier "?
febrero59 # :
Excuse me Vladimir, what do I have to do so that I don't get the error: " 'label' - undeclared identifier "?
Excuse me Vladimir, what do I have to do so that I don't get the error: " 'label' - undeclared identifier "?
Note that you have declared an object inside the panel class:
class CControlsDialog : public CAppDialog { private: CLabel m_label; // CLabel object
Now you need to organize a method by which your adviser will communicate with the panel class.
One more request - attach the code as a file (using the button
), so it will be more convenient for me to download your code.
Example:
//+------------------------------------------------------------------+ //| Test Panel.mq5 | //| Copyright © 2022, Vladimir Karputov | //| https://www.mql5.com/en/users/barabashkakvn | //+------------------------------------------------------------------+ #property copyright "Copyright © 2022, Vladimir Karputov" #property link "https://www.mql5.com/en/users/barabashkakvn" #property version "1.00" //--- input parameters input int Input1=9; #include <Controls\Dialog.mqh> #include <Controls\Label.mqh> //+------------------------------------------------------------------+ //| defines | //+------------------------------------------------------------------+ //--- indents and gaps #define INDENT_LEFT (11) // indent from left (with allowance for border width) #define INDENT_TOP (11) // indent from top (with allowance for border width) #define INDENT_RIGHT (11) // indent from right (with allowance for border width) #define INDENT_BOTTOM (11) // indent from bottom (with allowance for border width) #define CONTROLS_GAP_X (5) // gap by X coordinate #define CONTROLS_GAP_Y (5) // gap by Y coordinate //--- for buttons #define BUTTON_WIDTH (100) // size by X coordinate #define BUTTON_HEIGHT (20) // size by Y coordinate //--- for the indication area #define EDIT_HEIGHT (20) // size by Y coordinate //--- for group controls #define GROUP_WIDTH (150) // size by X coordinate #define LIST_HEIGHT (179) // size by Y coordinate #define RADIO_HEIGHT (56) // size by Y coordinate #define CHECK_HEIGHT (93) // size by Y coordinate //+------------------------------------------------------------------+ //| Class CControlsDialog | //| Usage: main dialog of the Controls application | //+------------------------------------------------------------------+ class CControlsDialog : public CAppDialog { private: CLabel m_label; // CLabel 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); //--- Change text virtual void ChangeText(const string text) {m_label.Text(text);} protected: //--- create dependent controls bool CreateLabel(void); //--- handlers of the dependent controls events void OnClickLabel(void); }; //+------------------------------------------------------------------+ //| Event Handling | //+------------------------------------------------------------------+ EVENT_MAP_BEGIN(CControlsDialog) //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ EVENT_MAP_END(CAppDialog) //+------------------------------------------------------------------+ //| 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(!CreateLabel()) return(false); //--- succeed return(true); } //+------------------------------------------------------------------+ //| Create the "CLabel" | //+------------------------------------------------------------------+ bool CControlsDialog::CreateLabel(void) { //--- coordinates int x1=INDENT_RIGHT; int y1=INDENT_TOP+CONTROLS_GAP_Y; int x2=x1+100; int y2=y1+20; //--- create if(!m_label.Create(m_chart_id,m_name+"Label",m_subwin,x1,y1,x2,y2)) return(false); if(!m_label.Text("Label123456")) return(false); if(!Add(m_label)) return(false); Comment(m_label.Text()); //--- succeed return(true); } //+------------------------------------------------------------------+ //| Global Variables | //+------------------------------------------------------------------+ CControlsDialog ExtDialog; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- create application dialog if(!ExtDialog.Create(0,"Controls",0,40,40,380,344)) return(INIT_FAILED); //--- run application ExtDialog.Run(); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- destroy dialog ExtDialog.Destroy(reason); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- MqlTick STick; if(SymbolInfoTick(Symbol(),STick)) { ExtDialog.ChangeText(DoubleToString(STick.ask,Digits())); } } //+------------------------------------------------------------------+ //| Expert chart event function | //+------------------------------------------------------------------+ 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); } //+------------------------------------------------------------------+
Result:

Files:
Test_Panel.mq5
15 kb
Thanks again Vladimir. Little by little I am discovering how the classes work.
Thank you very much.