Autor Beitrag
mick
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 159



BeitragVerfasst: So 01.06.08 11:59 
hallo zusammen,

ich habe eine kleine delphi-frage, vllt. weiss jmd. weiter. folgender code gegeben:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
  if Std div 32 = 0 then ShpStd32Akt.Brush.Color := FarbeInaktiv
    else begin
      ShpStd32Akt.Brush.Color := FarbeAktiv;
      Std := Std - 32;
    end;
  if Std div 16 = 0 then ShpStd16Akt.Brush.Color := FarbeInaktiv
    else begin
      ShpStd16Akt.Brush.Color := FarbeAktiv;
      Std := Std - 16;
    end;
  if Std div 8 = 0 then ShpStd8Akt.Brush.Color := FarbeInaktiv
    else begin
      ShpStd8Akt.Brush.Color := FarbeAktiv;
      Std := Std - 8;
    end;
  if Std div 4 = 0 then ShpStd4Akt.Brush.Color := FarbeInaktiv
    else begin
      ShpStd4Akt.Brush.Color := FarbeAktiv;
      Std := Std - 4;
    end;
  if Std div 2 = 0 then ShpStd2Akt.Brush.Color := FarbeInaktiv
    else begin
      ShpStd2Akt.Brush.Color := FarbeAktiv;
      Std := Std - 2;
    end;
  if Std div 1 = 0 then ShpStd1Akt.Brush.Color := FarbeInaktiv
    else begin
      ShpStd1Akt.Brush.Color := FarbeAktiv;
      Std := Std - 1;
    end;


es geht um die darstellung einer binaeruhr. die shapes sind die "lichter". mit den if-anweisungen rechne ich aus, welche lichter "brennen" sollen und welche nicht. wie man siehst, ist das viel aehnlicher code. hinzu kommt, dass ich das gleiche ja nochmal fuer die minuten und sekunden mache. das sieht ineffizient aus. kann ich das in einer art schleife oder in einer separaten prozedur auslagern? das waere schoen. es gibt nur ein problem: die bezeichnernamen fuer die shapes sind jedes mal andere: ShpStd32Akt.Brush, ShpStd16Akt.Brush, ShpStd8Akt.Brush, ... kurzum:

kann ich in delphi einen bezeichnernamen --> im quellcode <-- durch eine variable dynamisch ergaenzen? z.b.:

ShpStdXAkt.Brush
und fuer X wird dann mit jedem z.b. prozeduraufruf ein individueller wert eingesetzt.


besten dank im voraus,
mick.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: So 01.06.08 12:06 
Schau dir mal FindComponent an. ;-)

_________________
We are, we were and will not be.
Grenzgaenger
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 01.06.08 12:39 
weshalb nimmste nicht die addressen in 'n array / recrods oder objektliste... und iterierst dann mit 'n bischen logik durch... damit du immer die richtigen referenzierst...??

so in etwa
ausblenden Delphi-Quelltext
1:
2:
for lichter in lichtlist do
 dosomething(lichter.minuten);
mick Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 159



BeitragVerfasst: So 01.06.08 12:43 
ja, beides sollte gut klappen - super, danke!

ciao,
mick.
mick Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 159



BeitragVerfasst: So 01.06.08 14:06 
hm, dann haette ich jetzt jedoch noch eine ergaenzungsfrage:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure TForm1.ShpStd32ProgMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if ShpStd32Prog.Brush.Color = FarbeInaktiv then ShpStd32Prog.Brush.Color := FarbeAktiv
    else ShpStd32Prog.Brush.Color := FarbeInaktiv;
end;

{ **************************************************************************** }

procedure TForm1.ShpStd16ProgMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if ShpStd16Prog.Brush.Color = FarbeInaktiv then ShpStd16Prog.Brush.Color := FarbeAktiv
    else ShpStd16Prog.Brush.Color := FarbeInaktiv;
end;

...


habe also meine sagen wir mal 16 shapes. immer, wenn eines angeklickt wird, soll sich die farbe aendern (in FarbeAktiv oder FarbeInaktiv (konstanten) - je nach vorigem status). soweit, so gut. aber wieder viel code, oben sind ja nur 2 beispiele als ausschnitt.

es waere schoen, wenn ich bei jedem klick auf ein shape auf die selbe ereignismethode verweisen koennte im objektinspektor. dazu muesste aber im code der bezeichnername der jeweiligen shape-komponente, die ich angeklickt habe, ausgelesen werden, damit die richtige shape ihre farbe aendert.

also: wie kann man beim klick auf eine komponente ihren eigenen namen als string auslesen? und das auch, wenn ich beim klick auf verschiedene shapes auf stets ein und dieselbe ereignismethode verweise?
moechte es mit findcomponent loesen, wenn moeglch. ist die loesung irgendetwas mit "self"? kenne mich nicht genug aus.

danke erneut im voraus,

ciao,
mick.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: So 01.06.08 14:09 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
procedure TForm1.ShpStd16ProgMouseDown(Sender: TObject; 
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer); 
begin 
  if (Sender as tShape).Brush.Color = FarbeInaktiv then (Sender as tShape).Brush.Color := FarbeAktiv 
    else (Sender as tShape).Brush.Color := FarbeInaktiv; 
end;

_________________
We are, we were and will not be.
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: So 01.06.08 14:09 
Viel einfacher als du denkst :D

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.ShpStd16ProgMouseDown(Sender: TObject;  
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);  
begin  
  if (Sender as TShape).Brush.Color = FarbeInaktiv then
    (Sender as TShape).Brush.Color := FarbeAktiv  
  else
    (Sender as TShape.Brush.Color := FarbeInaktiv;  
end;


Nein, ich ignoriere den orangenen Hinweis :-(

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Grenzgaenger
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 01.06.08 14:31 
sag mal, weshalb willste denn so viele methoden ersellen? erstell doch eine und weise sie den objekte als ereignismethode zu ... jedes mal, wenn dann das ereignis gefeuert wird, wird die selbe methode durchlaufen ... :-)

in etwa so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
...
private
 procedure mousedown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); 
 ...


myComp.OnMouseDown := MouseDown;


und schon läufts :-)
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: So 01.06.08 14:34 
Das macht er doch jetzt so: er will ja nur noch eine Ereignisprozedur verwenden :gruebel:

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
mick Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 159



BeitragVerfasst: So 01.06.08 17:19 
funktioniert wunderbar - danke zum zweiten!

ciao,
mick.