Autor Beitrag
gd0123456
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Sa 21.09.13 02:32 
Hi, warum kann ich mittels (Sender as Tlabel).top die Position nicht ändern. Beim Ausführen schreibt er ungültige Typenumwandlung.

ausblenden volle Höhe 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:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
type
  tlabel2 = class(tlabel)
    public
      procedure mouseclick(sender: tobject);
  end;

var
  Form1: TForm1;
  tl: tlabel2;
  counter: integer = 0;

procedure tlabel2.mouseclick(sender: tobject);
begin
   form1.Caption := (sender as tlabel2).name;
   form1.edit5.text := (sender as tlabel2).Caption;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin

   if (key = 37then
     tl.left := tl.left -1 <-- Funktioniernt
   else if (key = 38then
     (sender as tlabel2).top := (sender as tlabel2).top -1 <-- Ungültige Typenumwandlung
   else if (key = 39then
    (sender as tlabel2).left := (sender as tlabel2).left +1
   else if (key = 40then
     (sender as tlabel2).top := (sender as tlabel2).top +1;
end;

procedure TForm1.ToolButton1Click(Sender: TObject);
begin
  tl := tlabel2.Create(nil);
  tl.name := 'test'+inttostr(counter);
  tl.left := 200+counter*2;
  tl.top := 300+counter*4;
  tl.Caption := 'xxxxxx'+inttostr(counter)+' '+tl.name;
  tl.width := 50;
  tl.height := 100;
  tl.Parent := form1;

  tl.OnClick := tl.mouseclick;
  inc(counter);
end;



Moderiert von user profile iconNarses: Topic aus Delphi Language (Object-Pascal) / CLX verschoben am Sa 21.09.2013 um 10:23
Gerd Kayser
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 632
Erhaltene Danke: 121

Win 7 32-bit
Delphi 2006/XE
BeitragVerfasst: Sa 21.09.13 13:45 
user profile icongd0123456 hat folgendes geschrieben Zum zitierten Posting springen:
Hi, warum kann ich mittels (Sender as Tlabel).top die Position nicht ändern. Beim Ausführen schreibt er ungültige Typenumwandlung.

Weil Du Äpfel mit Birnen vergleichst. :-)
Setze mal in die Prozedur FormKeyDown Folgendes ein:
ausblenden Delphi-Quelltext
1:
  ShowMessage(Sender.ClassName);					

Dann siehst Du, dass Sender hier niemals ein TLabel oder TLabel2 sein kann.
gd0123456 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Sa 21.09.13 22:31 
Ach danke mal für den Hinweis.

Na gut und wie kann ich das Problem jetzt lösen damit ich auf das Tlabel2 zugreifen kann? Mann kann ja mehrere Label2 erzeugen und je nachdem welches geklickt wurde würde ich es gerne bearbeiten können oder eben verschieben. Dachte eben mit Sender as Tlabel2 würde es funktionieren aber logischer weiße eben nicht wenn die onKeydown Methode von Tform ist. Bitte um kurz Hilfe oder Denkanstoss.
Gerd Kayser
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 632
Erhaltene Danke: 121

Win 7 32-bit
Delphi 2006/XE
BeitragVerfasst: Sa 21.09.13 22:42 
user profile icongd0123456 hat folgendes geschrieben Zum zitierten Posting springen:
Na gut und wie kann ich das Problem jetzt lösen damit ich auf das Tlabel2 zugreifen kann?

TLabel hat doch bereits ein OnClick-Ereignis. Da brauchst Du doch kein TLabel2. Dieses Ereignis weist Du den erzeugten Labels zu. Eine Ereignisprozedur für alle Labels reicht. Innerhalb der Prozedur kannst Du dann mit "Sender as TLabel" arbeiten.
gd0123456 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Sa 21.09.13 22:47 
Mittels Pfeiltasten soll das Label positioniert werden können.
Da das Label kein onKeydown besitzt muss ich es ja über das Form Keydown machen. oder sitze ich jetzt auf der Leitung?
Gerd Kayser
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 632
Erhaltene Danke: 121

Win 7 32-bit
Delphi 2006/XE
BeitragVerfasst: Sa 21.09.13 22:54 
user profile icongd0123456 hat folgendes geschrieben Zum zitierten Posting springen:
Mittels Pfeiltasten soll das Label positioniert werden können.

Im OnClick-Ereignis den Sender speichern, z. B. "FSender := Sender". Im Ereignis FormKeydown dann mit dieser Variablen arbeiten ("(FSender as TLabel).Top").
gd0123456 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: Sa 21.09.13 23:04 
Danke vielsmals, hätte ich jetzt ehrlich gesagt auch selbst draufkommen können, aber ist ein Neugebiet für mich. Manche Sache sind doch einfacher als man denkt. Herzlichen Dank nochmal.
gd0123456 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: So 22.09.13 23:52 
HI hab da noch eine Frage,

ich erstelle in Form1 das Tlabel2. Von der Form3 möchte ich mittels (form1.findcomponent('feld1') as tlabel2).caption darauf zugreifen bzw. auslesen. Jedoch wird die Komponente nicht gefunden, wenn ich es mit einem Label mache welches ich auf die Form platziere dann findet er es.

Woran könnte es liegen. Danke für die Hilfe.

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:
//Label wird erstelltn in der Form1

procedure TForm1.ToolButton1Click(Sender: TObject);
var
  fstyle: string;
begin

  tl := tlabel2.Create(nil);

  tl.name := 'feld'+inttostr(counter);
  tl.left := 200+counter*2;
  tl.top := 300+counter*4;
  tl.Caption := '<feld>'+inttostr(counter)+' '+tl.name;
  tl.Parent := form1;
  tl.autosize := true;
  tl.Transparent := true;
  tl.Hint := 'X:'+inttostr(tl.Left)+' Y:'+inttostr(tl.Top);
  tl.ShowHint := true;

  tl.OnClick := tl.mouseclick;

  inc(Counter);

 end;

//Findcomponent in der Form3
   showmessage((form1.findcomponent('feld1'as TLabel2).caption);

Danke für die Unterstützung
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19314
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 22.09.13 23:54 
Du legst es nicht in die Liste der Komponenten, die deinem Formular gehören (du übergibst dem Konstruktor als Besitzer nil). Deshalb kannst du es darin auch nicht finden.
gd0123456 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 33



BeitragVerfasst: So 22.09.13 23:59 
Ok also hab jetzt beim Constructor nicht Nil eingetragen sondern form1 (denk mal dass dies richtig ist). Trotzdem findet er die Komponente nicht.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19314
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 23.09.13 05:28 
user profile icongd0123456 hat folgendes geschrieben Zum zitierten Posting springen:
Ok also hab jetzt beim Constructor nicht Nil eingetragen sondern form1 (denk mal dass dies richtig ist). Trotzdem findet er die Komponente nicht.
Ich vermute eher Self wäre richtig.
Sollte es trotzdem nicht gehen, liegt das an deinem TLabel2. Was das macht, weiß ich ja nicht.