Ver Mensaje Individual
  #9  
Antiguo 24-11-2006
Avatar de Neftali [Germán.Estévez]
Neftali [Germán.Estévez] Neftali [Germán.Estévez] is online now
[becario]
 
Registrado: jul 2004
Ubicación: Barcelona - Espańa
Posts: 18.286
Reputación: 10
Neftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en bruto
Cita:
Empezado por pborges36
...espero su opinion, comentarios y sugerencias para que vayamos mejorandolo.
Código Delphi [-]
Procedure TBRGFocusAdmin.AplicarColor(Color :TColor; Componente: TWinControl);
begin
  try
    if (Componente is TCustomEdit)     then (Componente as TEdit).Color := Color;
    if (Componente is TDateTimePicker) then (Componente as TDateTimePicker).Color:= Color;
    if (Componente is TCustomMemo)     then (Componente as TMemo).Color:= Color;
    if (Componente is TCustomComboBox) then (Componente as TComboBox).Color:= Color;

    (Componente as TWinControl).Repaint;
  except
  end;
end;

Creo que esto se puede optimizar (para cualquier componente que tenga esa propiedad) utilizando RTTI. Lo bueno de esto es que generalizando un poco la siguiente función se puede hacer para cualquier propiedad.

Código Delphi [-]
procedure TForm1.Button2Click(Sender: TObject);
var
  i:Integer;

  //·······················································································
  // Comprueba si existe una propiedad con ese nombre
  function ExistProp(Instance: TObject; const PropName: string):Boolean;
  var
    PropInfo: PPropInfo;
  begin
    // Busca la propiedad y deviuelve la estructura nil
    PropInfo := GetPropInfo(Instance, PropName);
    Result := not (PropInfo = nil);
  end;
  //·······················································································
  // Cambia el valor de la propiedad
  function SetPropAsString(AObj: TObject; const PropName, Value: String):Boolean;
  var
    PInfo: PPropInfo;
  Begin
    // Intentamos acceder (con un puntero) a la info. de la propiedad
    PInfo := GetPropInfo(AObj.ClassInfo, PropName);
    Result := PInfo <> nil;

    // Se ha obtenido la información...
    if (Result) then begin
      // Se ha encontrado la propiedad con éste nombre; Chequear el tipo...
      if (PInfo^.Proptype^.Kind = tkString) or
         (PInfo^.Proptype^.Kind = tkLString) then begin
        // Asignar el valor de tipo String
        SetStrProp(AObj, PInfo, Value);
      end
      else if (PInfo^.Proptype^.Kind = tkInteger) then begin
        // Asignar el valor...
        if (PInfo^.PropType^.Name = 'TColor') then begin
          SetOrdProp(AObj, PInfo, StringToColor(Value));
        end
        else begin
          SetOrdProp(AObj, PInfo, StrToInt(Value));
        end;
      end
      else begin
        Result := False;
        MessageDlg('''La propiedad '' + PropName + '' no es de tipo String (o un tipo implementado)', mtWarning, [mbOK], 0);
      end;
    end
    else begin
      // No se ha encontrado la propiedad con ese nombre
      Result := False;
    end;
  end;
  //·······················································································


begin
  // recorrer los componentes
  for i := 0 to (Self.ComponentCount - 1) do begin
    // existe esa propiedad
    if ExistProp(Self.Components[i], 'Color') then begin
      // Asignar valor
      SetPropAsString(Self.Components[i], 'Color', 'clInfoBk');
    end;
  end;
end;
__________________
Germán Estévez => Web/Blog
Guía de estilo, Guía alternativa
Utiliza TAG's en tus mensajes.
Contactar con el Clubdelphi

P.D: Más tiempo dedicado a la pregunta=Mejores respuestas.
Responder Con Cita