Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - OnClick bei TImage


Hochhäusl - Do 17.12.09 19:56
Titel: OnClick bei TImage
Hallo,

ich erstelle eine TImage und brauche ein OnClick-Ereignis:


Delphi-Quelltext
1:
2:
3:
4:
Bild:=TImage.Create(Form1);
Bild.Parent:= Form1;

Bild.OnClick:=BildClick;



Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.BildClick(Sender: TObject);
begin
Bild.Canvas.Ellipse(x1,y1,x2,y2);
end;


Das OnClick- Ereignis funktioniert auch, solange ich nicht versuche etwas zu Zeichnen.

Weiß Jemand wo mein Fehler liegt?


Lannes - Do 17.12.09 20:17

Hallo,

funktioniert einwandfrei.

Wo/wie ist die Variable Bild deklariert?

Was bedeutet "funktioniert nicht", wird das Ereignis nicht ausgelöst, wird nichts gezeichnet, gibt es eine Fehlermeldung ...?


Hochhäusl - Do 17.12.09 20:28

Ich bekomme eine Fehlermeldung:

Zitat:
Access violation at address 0042.. in module project.exe.
Read of address 0002C



Die Variable habe ich in der Unit deklariert:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
var
  Form1: TForm1;
  Bild: TBitmap;

implementation

{$R *.dfm}


Gausi - Do 17.12.09 20:29

Hm, was ist denn "Bild" denn nun? Ein TImage oder ein TBitmap? Da geht wohl was durcheinander...


Hochhäusl - Do 17.12.09 20:32

Stimmt, das habe ich wohl übersehen. :(

Habe jetz

Delphi-Quelltext
1:
Bild: TImage;                    


Ändert aber nichts am Problem.


Gausi - Do 17.12.09 20:37

Hm, ist Bild vielleicht nochmal als lokale Variable deklariert, in der Prozedur, wo das erstellt wird? Denn sonst hätte der Code vorher gar nicht kompilieren dürfen...

Zeig aber mal am besten den ganzen Code - so ist das nur ne Rumraterei. ;-)


Hochhäusl - Do 17.12.09 23:01

Hier der ganze Quelltext:


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:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button2: TButton;
    procedure BildClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Bild: TImage;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
var Bild: TImage;
begin
Bild:=TImage.Create(Form1);
Bild.Parent:= Form1;
Bild.Top:=10;
Bild.Left:=10;
Bild.Width := Image1.Width;
Bild.Height := Image1.Height;
Bild.OnClick:=BildClick;
end;

procedure TForm1.BildClick(Sender: TObject);
begin
Bild.Canvas.Ellipse(10,10,50,50);
end;

end.


Hochhäusl - Do 17.12.09 23:06

So, jetz hab ich den Fehler gefunden.

Hatte die Variable wirklich noch ein 2tes mal in der Procedure deklariert.
Einfach die Zeile 30 im obigen Code gelöscht, und schon funktioniert.

Danke für die schnelle Hilfe