跳至内容

ObjectName

ObjectName

该函数通过对象列表中的索引返回相应对象的名称。

string  ObjectName(
   int   object_index   // object index
   );

参数

object_index

[in] 对象索引。该值必须大于等于0且小于ObjectsTotal()的值。

返回值

成功时返回对象的名称。要获取详细的错误信息,需要调用GetLastError()函数。

示例:

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   int i;
   long current_chart_id=ChartID();
//--- creates several objects of label type
   for(i=0; i<300; i+=10)
     {
      string obj_name="label_object"+IntegerToString(i);
      //--- creating label object (it does not have time/price coordinates)
      if(ObjectCreate(obj_name,OBJ_LABEL,0,0,0))
        {
         PrintFormat("Object %s created.",obj_name);
         //--- set random color
         ObjectSetInteger(current_chart_id,obj_name,OBJPROP_COLOR,MathRand());
         //--- set text property
         ObjectSetString(current_chart_id,obj_name,OBJPROP_TEXT,StringFormat("Simple Label at y= %d",i));
         //--- set distance property
         ObjectSet(obj_name,OBJPROP_XDISTANCE,i);
         ObjectSet(obj_name,OBJPROP_YDISTANCE,i);
         //--- forced chart redraw
         ChartRedraw(current_chart_id);
         Sleep(10);
        }
      else
        {
         Print("Error: can't create label! code #",GetLastError());
        }
     }
//--- sleep to see the objects created
   Sleep(3000);
//--- show all objects
   int obj_total=ObjectsTotal();
   PrintFormat("Total %d objects",obj_total);
   string name;
   for(i=0;i<obj_total;i++)
     {
      name=ObjectName(i);
      PrintFormat("%d object: Object name - %s",i,name);
     }
//--- delete all objects
   ObjectsDeleteAll();
  }
最后更新于