Autor Beitrag
reptile
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 40



BeitragVerfasst: Mo 01.01.07 17:25 
ich hoffe mal dass ich das jetzt in die richtige sparte schreibe, gefunden hab ich nichts dazu..^^

also: ich hab auf meiner form 81 labels, diese werden aber mit create erst zur laufzeit erzeugt.nun möchte ich diesen labels irgendwie ein onmousedown-ereignis zuweisen. wie mache ich das am besten, im objektinspektor kann ich das ja nicht einstellen, weil sie ja erst erzeugt werden. außerdem müsste ich auch noch irgendwie abfragen können auf welches label geklickt wurde, wenn möglich sollte es aber trotzdem bei nur einem mousedown ereignis bleiben.

hilfe wäre nett, am besten so erklären dass es auch ein anfänger versteht ;)
rey003
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68

Win XP
BDS 2006
BeitragVerfasst: Mo 01.01.07 17:40 
Hallo,
also ich hätte mir eine eigene Procedure geschrieben (z.B.: mit dem Namen: LabelsOnMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer)
Beim Erstellen der Labels weißt du dann deiner erstellten Komponente die Funktion zu!
Das machst du mit: Label.OnMouseDown := @LabelsOnMouseDown!
Nun kannst du in deiner Procedure über If-Schleifen oder Case-Schleifen mit Hilfe des übergebenen Senders unterscheiden von welcher Komponente(Label) der Befehl kam!
So ich hoffe das du verstehst wie ich das alles meinte!

_________________
Daniel S.
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: Mo 01.01.07 17:47 
Aijaiaiaiaia... If-Schleifen... Das tut weh :cry: [sowas gibt es nicht ;-)]

Zur Label-Unterscheidung:
(Sender as TLabel).Name würde den Namen des Labels zurückgeben, auf das geklickt wurde.

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



BeitragVerfasst: Mo 01.01.07 17:50 
danke das versteh ich soweit und es scheint mir auch die richtige lösung aber zu der zeile "Labelneu.OnMouseDown := @LabelsOnMouseDown;" sagt er mir :
incompatible types : 'TMouseEvent' and 'Pointer'

hab ich die procedure vielleicht falsch deklariert ? ich hab sie einfach oben über die variablen geschrieben.
rey003
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68

Win XP
BDS 2006
BeitragVerfasst: Mo 01.01.07 17:53 
hmm... hatte ich fast vermutet! :(
Da muss ich selber erstmal probieren!

@GTA-Place:
Ich habe nicht gelesen wie viele Labels es sind! Aber irgendwie muss er ja trotzdem abfragen von welchem Label es kommt!

_________________
Daniel S.
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: Mo 01.01.07 17:55 
Mach das @ vor LabelsOnMouseDown weg.

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



BeitragVerfasst: Mo 01.01.07 17:57 
dann ist die fehlermeldung leicht abgeändert :
incompatible types : 'method pointer and regular procedure'

was bewirkt denn das @ ?
rey003
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68

Win XP
BDS 2006
BeitragVerfasst: Mo 01.01.07 17:59 
hmm..
ausblenden Delphi-Quelltext
1:
Labelneu.OnMouseDown := TMouseEvent(@LabelsMouseDown);					

Das müsste eigentlich gehen! Tut es aber irgendwie nicht! :?:

_________________
Daniel S.
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: Mo 01.01.07 18:20 
user profile iconreptile hat folgendes geschrieben:
was bewirkt denn das @ ?

Dass es zum Pointer wird.

Hast du es so gemacht (eben etwas abergeändert)?:
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:
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure MyOwnButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.MyOwnButtonClick(Sender: TObject);
begin
  ShowMessage('Hallo!');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Button1.OnClick := MyOwnButtonClick;
end;

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



BeitragVerfasst: Mo 01.01.07 18:30 
hmmm
wenn ich es so mache krieg ich folgende fehlermeldung

-bei der deklaration der procedure steht :
unsatisfied forward or external declaration :'TForm1.LabelsOnMouseDown'

...kann doch nicht so schwer sein ^^ hab übrigens delphi 6 wenn das was ändert.
rey003
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68

Win XP
BDS 2006
BeitragVerfasst: Mo 01.01.07 18:36 
GTA-Place hatte schon recht!
So in etwa müsste es bei dir aussehen:
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:
type
  TForm1 = class(TForm)
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure LabelsMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.LabelsMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 showmessage('Es geht!');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 //Zuweisung (bei mir jetzt als Bsp Label1
 Label1.OnMouseDown:=LabelsMouseDown;
end;


//Edit: Zu deiner Fehlermeldung! Hast du die Deklaration oben und unten dann im eigentlichen Quelltext gleich geschrieben? (das könnte deine Fehlermeldung erklären)

_________________
Daniel S.
reptile Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 40



BeitragVerfasst: Mo 01.01.07 18:45 
genauso siehts bei mir jetzt auch aus aber es scheitert eben an der deklaration der procedure labelsOnMouseDown, da steht: unsatisfied forward or external declaration.
beim ganzen rest kommen gar keine fehlermeldungen mehr also
Labelneu.OnMouseDown := LabelsOnMouseDown; funktioniert soweit.

ich hab die procedure jetzt schon unter "type", "private" und "public" geschrieben aber jedesmal steht diese meldung da.

EDIT: ja ist gleich geschrieben.
rey003
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68

Win XP
BDS 2006
BeitragVerfasst: Mo 01.01.07 18:48 
Am Besten du zeigst mal deinen Quelltext! Oder zumindest die betreffenden Stücke! :wink:

_________________
Daniel S.
reptile Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 40



BeitragVerfasst: Mo 01.01.07 18:55 
kein problem ist ja noch nicht besonders lang ^^ die button-procedure kann vernachlässigt werden:

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:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, uniterstellen;

type
  TForm1 = class(TForm)
    Edittest: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
    procedure LabelsMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  end;



var        zahlen : array[1..9 , 1..9] of integer;
           posx,posy,durchzaehlen : integer;
           labelneu: tlabel;

  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
randomize;
zahlen[1,1] := random(9);
edittest.text := inttostr(zahlen[1,1]);
uniterstellen.erstellen;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
posx := 40;
posy := 40;
for durchzaehlen := 1 to 81 do
begin
labelneu:=Tlabel.Create(Self);
  with labelneu do
  begin
  name := ('label')+ inttostr(durchzaehlen);
      Parent:=Form1;
      Left:=posx;
      Top:=posy;
      Width:= 20;
      height := 37;
      labelneu.caption := inttostr(durchzaehlen);

      Labelneu.OnMouseDown := LabelsMouseDown;
      if posx <> 360 then posx := posx + 40
      else begin posx := 40;posy := posy +40 end;
  end;
end;

end;

procedure LabelsMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

end;


end.
rey003
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68

Win XP
BDS 2006
BeitragVerfasst: Mo 01.01.07 19:01 
Das TForm1. vor dem LabelsMouseDown fehlt!
So also:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.LabelsMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);  
begin  
 
end;

_________________
Daniel S.
reptile Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 40



BeitragVerfasst: Mo 01.01.07 19:05 
:oops: hehehe
danke sehr jetzt geht alles, auch die sender-sache funktioniert.