Input field properties: text alignment and read-only

For objects of type OBJ_EDIT (input field), an MQL program can set two specific properties defined using the ObjectSetInteger/ObjectGetInteger functions.

Identifier

Description

Value type

OBJPROP_ALIGN

Horizontal text alignment

ENUM_ALIGN_MODE

OBJPROP_READONLY

Ability to edit text

bool

The ENUM_ALIGN_MODE enumeration contains the following members.

Identifier

Description

ALIGN_LEFT

Left alignment

ALIGN_CENTER

Center alignment

ALIGN_RIGHT

Right alignment

Note that, unlike OBJ_TEXT and OBJ_LABEL objects, the input field does not automatically resize itself to fit the text entered, so for long strings, you may need to explicitly set the OBJPROP_XSIZE property.

In the edit mode, horizontal text scrolling works inside the input field.

The ObjectEdit.mq5 script creates four OBJ_EDIT objects: three of them are editable with different text alignment methods and the fourth one is in the read-only mode.

#include "ObjectPrefix.mqh"
   
void SetupEdit(const int xconst int yconst int dxconst int dy,
   const ENUM_ALIGN_MODE alignment = ALIGN_LEFTconst bool readonly = false)
{
   // create an object with a description of the properties
   const string props = EnumToString(alignment)
      + (readonly ? " read-only" : " editable");
   const string name = ObjNamePrefix + "Edit" + props;
   ObjectCreate(0nameOBJ_EDIT000);
   // position and size
   ObjectSetInteger(0nameOBJPROP_XDISTANCEx);
   ObjectSetInteger(0nameOBJPROP_YDISTANCEy);
   ObjectSetInteger(0nameOBJPROP_XSIZEdx);
   ObjectSetInteger(0nameOBJPROP_YSIZEdy);
   // specific properties of input fields
   ObjectSetInteger(0nameOBJPROP_ALIGNalignment);
   ObjectSetInteger(0nameOBJPROP_READONLYreadonly);
   // colors (different depending on editability)
   ObjectSetInteger(0nameOBJPROP_BGCOLORclrWhite);
   ObjectSetInteger(0nameOBJPROP_COLORreadonly ? clrRed : clrBlue);
   // content
   ObjectSetString(0nameOBJPROP_TEXTprops);
   // tooltip for editable
   ObjectSetString(0nameOBJPROP_TOOLTIP,
      (readonly ? "\n" : "Click me to edit"));
}
   
void OnStart()
{
   SetupEdit(10010020020);
   SetupEdit(10012020020ALIGN_RIGHT);
   SetupEdit(10014020020ALIGN_CENTER);
   SetupEdit(10016020020ALIGN_CENTERtrue);
}

The result of the script is shown in the image below.

Input fields in different modes

Input fields in different modes

You can click on any editable field and change its content.