AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein GUI-Design mit VCL / FireMonkey / Common Controls Delphi Anzahl Formulare / Dynamisch Klassen ansprechen?
Thema durchsuchen
Ansicht
Themen-Optionen

Anzahl Formulare / Dynamisch Klassen ansprechen?

Ein Thema von miLeRiAm · begonnen am 31. Okt 2008 · letzter Beitrag vom 31. Okt 2008
Antwort Antwort
Seite 2 von 2     12   
angos

Registriert seit: 26. Mai 2004
Ort: Rheine
549 Beiträge
 
Delphi 11 Alexandria
 
#11

Re: Anzahl Formulare in der Applikation

  Alt 31. Okt 2008, 11:11
Hi,

um heruaszufinden ob ein Objekt eine Property hat, habe ich hier im Forum mal was gefunden gehabt:


Delphi-Quellcode:
uses
  TypInfo;
[...]

function HasProperty(AClass : TObject; APropertyName : String) : Boolean;
var
  MyPropInfo: PPropInfo;
begin
  MyPropInfo := GetPropInfo(AClass.ClassInfo, APropertyName);
  Result := MyPropInfo <> NIL;
end;

procedure TForm1.btnClick(Sender: TObject);
begin
  if HasProperty(MeinObjekt, 'Caption') then
  begin

  end;
end;
HTH
Ansgar
Ansgar
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.540 Beiträge
 
Delphi 11 Alexandria
 
#12

Re: Anzahl Formulare in der Applikation

  Alt 31. Okt 2008, 11:16
Abgesehen davon verstehe ich nicht, wieso 2 Schleifen nacheinander abgearbeitet werden.
Zitat:
Delphi-Quellcode:
...
   for i := 0 to screen.Forms[j].ComponentCount-1 do //erste Schleife
   begin
    screen.Forms[j].Components[i].Tag := ((j+1) * 10000) + i;
   end;

   for i := 0 to screen.Forms[j].ComponentCount-1 do //zweite Schleife
   begin

    comp := screen.Forms[j].Components[i];
...
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
miLeRiAm

Registriert seit: 3. Sep 2004
Ort: :/root
34 Beiträge
 
RAD-Studio 2009 Pro
 
#13

Re: Anzahl Formulare in der Applikation

  Alt 31. Okt 2008, 11:24
Und so wird der Code immer schöner.
HasProperty ist zwar super, allerdings hilft mir das nicht weite.

Delphi-Quellcode:
 if handleList.Count = 0 then
 begin

  for j := 0 to screen.FormCount-1 do
  begin

   for i := 0 to screen.Forms[j].ComponentCount-1 do
   begin

    comp := screen.Forms[j].Components[i];

    comp.Tag := ((j+1) * 10000) + i;

      if comp is TButton then handleList.Add(IntToStr(comp.Tag)+'='+TButton(comp).Caption);
      if comp is TToolButton then handleList.Add(IntToStr(comp.Tag)+'='+TToolButton(comp).Caption);
      if comp is TLabel then handleList.Add(IntToStr(comp.Tag)+'='+TLabel(comp).Caption);
      if comp is TMenuItem then handleList.Add(IntToStr(comp.Tag)+'='+TMenuItem(comp).Caption);
      if comp is TCheckBox then handleList.Add(IntToStr(comp.Tag)+'='+TCheckBox(comp).Caption);
      if comp is TRadioButton then handleList.Add(IntToStr(comp.Tag)+'='+TRadioButton(comp).Caption);
      if comp is TTabSheet then handleList.Add(IntToStr(comp.Tag)+'='+TTabSheet(comp).Caption);
      if comp is TSpeedButton then handleList.Add(IntToStr(comp.Tag)+'='+TSpeedButton(comp).Caption);

   end; // for i = components

  end; // for j = forms

 end; // if list is empty

Das Problem bleibt bestehen, dass ich in Delphi (bisher) keine Möglichkeit gefunden habe, Klassen dynamisch zuzuweisen, z.B. so:

TClass('TLabel').Caption
oder
TClass(comp.ClassName).Caption

Das kann in Delphi nie klappen, da man sofort die Klassentypen benötigt um auf etwas zuzugreifen.
Es wird nicht einfach compiliert in der Hoffnung der User weiß genau was er da gerade zusammenbaut.

Ideen?
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.540 Beiträge
 
Delphi 11 Alexandria
 
#14

Re: Anzahl Formulare / Dynamisch Klassen ansprechen?

  Alt 31. Okt 2008, 11:49
Basierend auf obigem Code habe ich mal diese Funktion gebastelt:
Delphi-Quellcode:
function CompHasCaption(AClass : TObject; out sResult: string): Boolean;
const sProp = 'Caption';
var
  MyPropInfo: PPropInfo;
begin
  sResult := '';
  MyPropInfo := GetPropInfo(AClass.ClassInfo, sProp);
  Result := Assigned(MyPropInfo);
  if Result then
    sResult := GetPropValue(AClass, sProp);
end;
Damit sollte es gehen.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
miLeRiAm

Registriert seit: 3. Sep 2004
Ort: :/root
34 Beiträge
 
RAD-Studio 2009 Pro
 
#15

Re: Anzahl Formulare / Dynamisch Klassen ansprechen?

  Alt 31. Okt 2008, 11:54
Delphi-Quellcode:
 if handleList.Count = 0 then
 begin

  for j := 0 to screen.FormCount-1 do

   for i := 0 to screen.Forms[j].ComponentCount-1 do
   begin

    comp := screen.Forms[j].Components[i];

    comp.Tag := ((j+1) * 10000) + i;

    if (CompHasCaption(Comp, s)) then handleList.Add(IntToStr(comp.Tag)+'='+s);

   end; // for i = components

 end; // if list is empty

Wie wenig code das nur noch ist

VIELEN lieben Dank, hat mich viele Nerven gekostet.


Für alle die noch was setzen wollen, so gehts:

Delphi-Quellcode:
function CompSetCaption(AClass : TObject; Caption: string): Boolean;
const sProp = 'Caption';
var
  MyPropInfo: PPropInfo;
begin
  MyPropInfo := GetPropInfo(AClass.ClassInfo, sProp);
  Result := Assigned(MyPropInfo);
  if Result then
  begin
   SetPropValue(AClass, sProp, Caption);
   result := true;
  end else
   result := false;
end;
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.540 Beiträge
 
Delphi 11 Alexandria
 
#16

Re: Anzahl Formulare / Dynamisch Klassen ansprechen?

  Alt 31. Okt 2008, 12:09
Kleiner Verbesserungsvorschlag:
Delphi-Quellcode:
function CompSetCaption(AClass : TObject; const Caption: string): Boolean;
const sProp = 'Caption';
var
  MyPropInfo: PPropInfo;
begin
  MyPropInfo := GetPropInfo(AClass.ClassInfo, sProp);
  Result := Assigned(MyPropInfo);
  if Result then
    SetPropValue(AClass, sProp, Caption);
end;
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
miLeRiAm

Registriert seit: 3. Sep 2004
Ort: :/root
34 Beiträge
 
RAD-Studio 2009 Pro
 
#17

Re: Anzahl Formulare / Dynamisch Klassen ansprechen?

  Alt 31. Okt 2008, 12:22
Vielen Dank, habs allesamt mal für die codelib vorgeschlagen

Sehr sehr praktisch.
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 2 von 2     12   


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 07:24 Uhr.
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