Autor Beitrag
Nightfly
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 163



BeitragVerfasst: Do 04.03.04 10:28 
Hi

also die Frage scheint mir so trivial das ich mich schon fast schäme,aber die Suche hat nix gebracht.

Also, ich habe ein Forumular mit mehreren Buttons. Ein klick auf irgendeinen dieser Buttons soll ein zweites Formular öfnen (bei jedem Button das gleiche). Aber das geöfnete Forumular soll als Überschrift den Namen des Buttons tragen,auf dessen klick hin es ausgeführt wurde.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure Thf.Button1Click(Sender: TObject);
begin
   Application.CreateForm(TForm2,Form2);
   Form2.Caption := Sender.Caption;
   Form2.BringToFront;
   Form2.Visible := true;
end;


Diesen Code wollte ich allen Buttons zufügen,aber mit dem Sender so wie ich mir das gedacht hab geht das nicht. KAnn mir jemand helfen?

Moderiert von user profile iconPeter Lustig: Code- durch Delphi-Tags ersetzt
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 04.03.04 10:36 
Hallo!

Der Sender ist vom Typ TObject und hat daher keine Eigenschaft "Caption". Wir haben ja aber den Vorteil, dass wir wissen, dass der Sender in Wahrheit ein Button ist. Also können wir nach TButton casten. Das machen wir so:

ausblenden Delphi-Quelltext
1:
2:
If Sender is TButton then //doch nochmal prüfen, ob das klappt
  Form2.Caption := TButton(Sender).Caption; //cast


MfG
Peter

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Nighti
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 19

Win XP
D5 Standard
BeitragVerfasst: Do 04.03.04 10:43 
oder du nimmst

ausblenden Delphi-Quelltext
1:
Form2.Caption := (Sender as TButton).Caption;					


müsste auch gehn

is halt ohne überprüfung aber wenn die prozedur ja eh nur mit buttons verlinkt is ...
Nightfly Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 163



BeitragVerfasst: Do 04.03.04 11:06 
dank euch!
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Do 04.03.04 12:37 
Nighti hat folgendes geschrieben:
is halt ohne überprüfung aber wenn die prozedur ja eh nur mit buttons verlinkt is ...

as überprüft intern auch per is ob der Typecast zulässig ist und löst (falls das nicht der Fall ist) eine Exception aus...

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!