Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Array con diferentes tipos (https://www.clubdelphi.com/foros/showthread.php?t=81420)

lunicirus 13-11-2012 01:19:15

Array con diferentes tipos
 
Hola quiero hacer un Array con diferentes tipos para poder desabilitarlos por partes rapidamente:

Código:

var
 RugosidadControl: Array[0..4] of TObjects;

begin

NombreArray[0]:= Label1;
NombreArray[1]:= Edit1;
NombreArray[2]:= label2;
NombreArray[3]:= Label3;
NombreArray[4]:= Edit2;

 if Ecuaciones[1] = true then
 begin
  for i:= 0 to 2 do
      i.Enabled := false;

  for j:= 3 to 4 do
      j.Enabled := true;
 end
 else begin
  for i:= 0 to 2 do
      i.Enabled := true;

  for j:= 3 to 4 do
      j.Enabled := false;
 end;

pero no se como hacer la declaracion del array, o si se puede hacer de esta forma.
Gracias

roman 13-11-2012 01:47:03

Puedes tener un arreglo de objetos, no hay problema. Pero para hacer asignaciones como las que indicas, debes partir de algún ancestro comuún que tenga la o las propiedades que deseas manipular. TObject no, pues no tiene la propiead Enabled. Quizá TControl o TWincontrol.

// Saludos

ecfisa 13-11-2012 01:52:54

Hola.

A ver, hay una gran confusión en ese código.

Si queres acceder a la propiead Enabled, tendrás que declarar a los elementos del arreglo de tipo TControl o de un descendiente de él:
Código Delphi [-]
type
  TNombreDeArray : array[0..4] of TControl;

var
  v: TNombreDeArray;
Luego sí podrás hacer:
Código Delphi [-]
  v[0]:= Label1;
  v[1]:= Edit1;
  ...
  v[0].Enabled := False;
  ...

Por otro lado, o la variable 'i' es de tipo Integer y por tanto carece de la propiedad Enabled o tendría que ser de tipo TControl o descendiente para que pudieras hacer:
Código Delphi [-]
  if ... then
    for i:= 0 to 2 do
      i.Enabled := false;
    ...
Pero eso nunca sucederá por que la variable de control de un ciclo for debe ser de tipo ordinal.

Sí podrías hacer algo como:
Código Delphi [-]
type
  TNombreDeArray = array[0..4] of TControl;

var
  v: TNombreDeArray;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  v[0]:= Label1;
  v[1]:= Edit1;
  v[2]:= Label2;
  ...
 
  if Ecuaciones[1] then
  begin
    for i:= 0 to 2 do
      v[i].Enabled:= False;
    ...

Saludos.

AzidRain 13-11-2012 01:56:29

Ya lo dijo Román, mejor no puede explicarse, el array que declaraste es un vil array de objetos. Ya pasando al tema purista, el array no es mas que un vil array (lo repito) de punteros a objetos, pero obviamente hay que acceder a esas direcciones de memoria utilizando la forma correcta.

nlsgarcia 13-11-2012 04:31:38

lunicirus,

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Button2: TButton;
    Label4: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//
// Ejemplo de Gestión de Arreglo de Controles
//

// Habilita los Controles Seleccionados
procedure TForm1.Button1Click(Sender: TObject);
var
   Control : Array[1..6] of TComponent;
   i : Integer;

begin

   // El arreglo contiene los nombres de los componentes a gestionar (Propiedad Name)
   Control[1] := Label1;
   Control[2] := Label2;
   Control[3] := Label3;
   Control[4] := Edit1;
   Control[5] := Edit2;
   Control[6] := Edit3;

   for i := 1 to 6 do
   begin
      if Control[i] is TLabel Then TLabel(Control[i]).Enabled := True;
      if Control[i] is TEdit Then TEdit(Control[i]).Enabled := True;
   end;

end;

// Desabilita los Controles Seleccionados
procedure TForm1.Button2Click(Sender: TObject);
var
   Control : Array[1..6] of TComponent;
   i : Integer;

begin

   // El arreglo contiene los nombres de los componentes a gestionar (Propiedad Name)
   Control[1] := Label1;
   Control[2] := Label2;
   Control[3] := Label3;
   Control[4] := Edit1;
   Control[5] := Edit2;
   Control[6] := Edit3;

   for i := 1 to 6 do
   begin
      if Control[i] is TLabel Then TLabel(Control[i]).Enabled := False;
      if Control[i] is TEdit Then TEdit(Control[i]).Enabled := False;
   end;

end;

end.
El código anterior permite asignar de forma manual que controles queremos gestionar por medio de un arreglo de controles para permitir habilitar y desabilitar los mismos.

Revisa las siguientes propiedades y métodos de controles:
Cita:

Propiedad Components : Matriz que contiene todos los componentes de un Form (Matriz de Punteros a Componentes).

Propiedad ComponentCount : Devuelve el número total de componentes de un Form.

Propiedad ComponentIndex: Índice de cada componente en la Matriz Components, permite accesar un componente por su índice.

Método FindComponent() : Permite accesar un Componente por su nombre.
Las anteriores propiedades y métodos son útiles para gestionar todos los componentes de un Form, ejemplo:
Código Delphi [-]
// Desabilita todos los Controles TLabel y TEdit de Form1
procedure TForm1.Button3Click(Sender: TObject);
var
   Control : TComponent;
   i : Integer;

begin
    for i := 0 to form1.ComponentCount - 1 do
    begin

       Control := FindComponent('Label' + IntToStr(i));
       if Control is TLabel then TLabel(Control).Enabled := False;

       Control := FindComponent('Edit' + IntToStr(i));
       if Control is TEdit then TEdit(Control).Enabled := False;

    end;
end;
Otra forma de hacer lo mismo que el código anterior:
Código Delphi [-]
// Desabilita todos los Controles TLabel y TEdit de Form1
procedure TForm1.Button4Click(Sender: TObject);
var
   i : Integer;

begin
    for i := 0 to form1.ComponentCount - 1 do
    begin
       if Components[i] is TLabel then TLabel(Components[i]).Enabled := False;
       if Components[i] is TEdit then TEdit(Components[i]).Enabled := False;
    end;
end;
Espero sea útil :)

Nelson.

lunicirus 13-11-2012 07:20:48

Vale muchas gracias me sirvio mucho!

cloayza 14-11-2012 14:23:46

Para estos menesteres me inclino por el uso de la RTTI

Código Delphi [-]
uses TypInfo;
...

function ExistProperty(AComp: TComponent; APropName: String):Boolean;  
var                                                                    
  PropInfo: PPropInfo;                                                 
begin                                                                  
     PropInfo := GetPropInfo(AComp.ClassInfo, APropName);              
     Result:=Assigned(PropInfo);                                       
end;                                                                   

procedure SetBooleanProperty(AComp: TComponent; APropName: String; AValue: Boolean);  
var                                                                                   
  PropInfo: PPropInfo;                                                                
begin                                                                                 
     PropInfo := GetPropInfo(AComp.ClassInfo, APropName);                             
     if PropInfo <> nil then                                                          
     begin                                                                            
          if PropInfo^.PropType^.Kind = tkEnumeration then                            
            SetOrdProp(AComp, PropInfo, Integer(AValue));                             
     end;                                                                             
end;                                                                                  

procedure TForm1.Button2Click(Sender: TObject);
var
   Control : Array[1..6] of TComponent;
   i : Integer;

begin

   // El arreglo contiene los nombres de los componentes a gestionar (Propiedad Name)
   Control[1] := Label1;
   Control[2] := Label2;
   Control[3] := Label3;
   Control[4] := Edit1;
   Control[5] := Edit2;
   Control[6] := Edit3;

   for i := 1 to 6 do
       if ExistProperty(Control[1],'Enabled') then
          SetBooleanProperty(Control[1],'Enabled', True);  
end;
Saludos cordiales


La franja horaria es GMT +2. Ahora son las 18:04:31.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi