![]() |
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:
Wobei natürlich zuvor aufWert an den jeweiligen Typen angepasst wird.
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; Mein Unwissen besteht jetzt darin, wie ich den in propertyname übergebenen Text so hinbekomme, daß es funktioniert. Also sowas wie findcomponent('TForm1') ...?? |
AW: Gibts sowas wie findproperty?
Hallo,
im einfachsten Falle könntest du deine Komponenten durchnummerieren:
Code:
und dann gehst du in einer (z.B. for-) Schleife deine Komponenten durch:
edit1
edit2 usw.
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; |
AW: Gibts sowas wie findproperty?
Hallo,
Zitat:
Vielleicht hilft folgender Ansatz Dein Problem zu lösen:
Delphi-Quellcode:
Gruß
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; |
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: |
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. |
AW: Gibts sowas wie findproperty?
Wie wäre es denn mit diesem hier?
Delphi-Quellcode:
(nur hingedaddelt, nicht getestet)
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; |
AW: Gibts sowas wie findproperty?
Hallo,
Zitat:
Delphi-Quellcode:
Gruß
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; |
AW: Gibts sowas wie findproperty?
Darf ich dazu noch ein paar Takte Generics einwerfen?
Delphi-Quellcode:
Damit ließe sich dein Code dann so schreiben (zwar etwas länger, aber deutlich flexibler und typsicher):
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;
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); |
AW: Gibts sowas wie findproperty?
Na, wenn das nicht cool ist :thumb:
|
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 01:17 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz