Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Gibts sowas wie findproperty? (https://www.delphipraxis.net/175655-gibts-sowas-wie-findproperty.html)

SearchBot 6. Jul 2013 22:52

Delphi-Version: XE

Gibts sowas wie findproperty?
 
Ich bin gerade am suchen, aber ich weiß nicht wie ich es nennen soll, daher fallen die Suchergebnisse daneben...

Ich möchte bei einer ganzen Reihe von TEdit die Eigenschaft text auf (leer) setzen. Also im Prinzip von (dasEdit).text:=''; Dann auch noch checked diverser TCheckbox auf false.

Also denke ich mir, ich mache eine

Code:
Procedure SetAll(propertyname:String; aufWert:string; vonComponents:array of TComponent);
Var i:integer;
begin
 for i:=0 to count(vonComponents)-1 do
  vonComponents[i].propertyname:=aufWert;
end;
Wobei natürlich zuvor aufWert an den jeweiligen Typen angepasst wird.

Mein Unwissen besteht jetzt darin, wie ich den in propertyname übergebenen Text so hinbekomme, daß es funktioniert.
Also sowas wie findcomponent('TForm1') ...??

MrMooed 6. Jul 2013 23:12

AW: Gibts sowas wie findproperty?
 
Hallo,

im einfachsten Falle könntest du deine Komponenten durchnummerieren:
Code:
edit1
edit2
usw.
und dann gehst du in einer (z.B. for-) Schleife deine Komponenten durch:
Delphi-Quellcode:
for i:=1 to EditCount do
  (TForm1.FindComponent('Edit' +IntToStr(i)) as TEdit).Text := '';
for i:=1 to CheckBoxCount do
  (TForm1.FindComponent('CheckBox' +IntToStr(i)) as TCheckBox).Checked := aufWert;

Volker Z. 6. Jul 2013 23:31

AW: Gibts sowas wie findproperty?
 
Hallo,

Zitat:

Gibts sowas wie findproperty?
Ist mir nichts bekannt.

Vielleicht hilft folgender Ansatz Dein Problem zu lösen:
Delphi-Quellcode:
procedure SetValue (C : array of TCheckBox; const Value : Boolean); overload;
var
  i : Integer;
begin
  for i := 0 to High (C) do
    C [i].Checked := Value
end;

procedure SetValue (C : array of TEdit; const Value : string); overload;
var
  i : Integer;
begin
  for i := 0 to High (C) do
    C [i].Text := Value
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  SetValue ([CheckBox1, CheckBox3], False);
  SetValue ([CheckBox2, CheckBox4], True);
  SetValue ([Edit1, Edit2], '');
  SetValue ([Edit3], 'Hallo Welt');
end;
Gruß

SearchBot 6. Jul 2013 23:37

AW: Gibts sowas wie findproperty?
 
Hallo,

@MrMooed:
ja diese Methode ist mir bekannt, mache ich auch schon immer so.
Aber im jetzigen Projekt haben die Felder ganz unterschiedliche Namen, weil sie auch für den Benutzer sichtbar werden - und ich möchte es halt gerne "vereinfachen" :)

@Volker Z.
Hm...
das mit Overload sieht gut aus...
Ich denke, das ist es.
Dankeschön :thumb:

jaenicke 7. Jul 2013 00:04

AW: Gibts sowas wie findproperty?
 
Ja, es gibt so etwas wie findproperty...

Nennt sich GetPropInfo aus der Unit TypInfo. Da bekommst du einen Pointer auf diese Information (PPropInfo), so dass du über PropertyInfo.PropType^.Kind z.B. den Typ der Property heraus bekommst. Mit SetPropValue kannst du den Wert einer Property setzen. Dafür musst du nicht unbedingt vorher die Information holen, da das rein über den Namen einfach so geht, aber ich mache das vorher um, wenn es eine Klasse ist, mir diese zu holen.

So kann ich auch Font.Name setzen, da ich zuerst feststelle, dass Font vom Typ tkClass existiert, mir das Objekt hole und SetPropValue dann auf dieses Objekt anwende, sprich dort Name setze.

nahpets 7. Jul 2013 00:18

AW: Gibts sowas wie findproperty?
 
Wie wäre es denn mit diesem hier?
Delphi-Quellcode:
procedure TForm1.SetAll(sText : string; bChecked : Boolean);
Var i : integer;
begin
  for i := 0 to ComponentCount - 1 do begin
         if Components[i] is TEdit then TEdit(Components[i]).Text := sText
    else if Components[i] is TCheckBox then TCheckBox(Components[i]).Checked := bChecked
    ;
  end;
end;
(nur hingedaddelt, nicht getestet)

Volker Z. 7. Jul 2013 01:01

AW: Gibts sowas wie findproperty?
 
Hallo,

Zitat:

Ja, es gibt so etwas wie findproperty [...] Nennt sich GetPropInfo aus der Unit TypInfo
jaenicke, vielen Dank für die Info :thumb:. Damit könnte man also meinen ersten Ansatz auch so umsetzen
Delphi-Quellcode:
uses
  System.TypInfo;

procedure SetValue (C : array of TObject; const Name : string; const Value : Variant);
var
  i : Integer;
  p : PPropInfo;
begin
  for i := 0 to High (C) do
    begin
      p := GetPropInfo (C [i], Name, []);
      if Assigned (p) then
        SetPropValue (C [i], Name, Value)
      else
        // mach was wenn nicht
    end
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  SetValue ([CheckBox1, CheckBox3], 'Checked', False);
  SetValue ([CheckBox2, CheckBox4], 'Checked', True);
  SetValue ([Edit1, Edit2], 'Text', '');
  SetValue ([Edit3], 'Text', 'Hallo Welt');
end;
Gruß

Uwe Raabe 7. Jul 2013 09:11

AW: Gibts sowas wie findproperty?
 
Darf ich dazu noch ein paar Takte Generics einwerfen?

Delphi-Quellcode:
uses
  System.Sysutils; // wegen TProc<T>
type
  TUtilities = record
    class procedure ForIn<T: class>(const Targets: array of T; DoProc: TProc<T>); static;
  end;

class procedure TUtilities.ForIn<T>(const Targets: array of T; DoProc: TProc<T>);
var
  instance: T;
begin
  for instance in Targets do
    DoProc(instance);
end;
Damit ließe sich dein Code dann so schreiben (zwar etwas länger, aber deutlich flexibler und typsicher):

Delphi-Quellcode:
  TUtilities.ForIn<TCheckBox>([CheckBox2, CheckBox4],
    procedure (ACheckBox: TCheckBox)
    begin
      ACheckBox.Checked := True;
    end);
  TUtilities.ForIn<TCheckBox>([CheckBox1, CheckBox3],
    procedure (ACheckBox: TCheckBox)
    begin
      ACheckBox.Checked := false;
    end);
  TUtilities.ForIn<TEdit>([Edit1, Edit2],
    procedure (AEdit: TEdit)
    begin
      AEdit.Text := '';
    end);
  TUtilities.ForIn<TEdit>([Edit3],
    procedure (AEdit: TEdit)
    begin
      AEdit.Text := 'Hallo Welt';
    end);

DeddyH 7. Jul 2013 09:16

AW: Gibts sowas wie findproperty?
 
Na, wenn das nicht cool ist :thumb:

SearchBot 7. Jul 2013 15:23

AW: Gibts sowas wie findproperty?
 
Boah, wenn ich auf die Postzeitpunkte schaue.. alles Nachtaktivisten :-D

Dankeschön für's mitdenken :thumb:

..und Generics sind mir zu abgehoben :shock:, das blick ich nicht, ist aber bestimmt voll praktisch :oops:


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:01 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz