Autor Beitrag
Mike_C
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 207

Win XP
D7 Enterprise
BeitragVerfasst: Mi 30.10.02 21:20 
OK, das wird wohl ein etwas länger Thread :wink:

Ich habe eine Klasse von TPanel abgeleitet und möchte dieser Klasse ein Image geben. das ist kein Problem, ich kann ja einfach eins erstellen. Mein Problem ist diesem Image dann ein ONCLICK-Event zur Laufzeit zuzuweisen.

Die Klasse sieht so aus (kompletter code)
ausblenden volle Höhe 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:
type
  TBubbleStone = class (TPanel)
  protected
    fForm, fColor: string;
    fImage: TImage;
    fIndex, fPoints: Integer;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Initialize(BallID: integer);
  published
    property BallForm: string read fForm write fForm;
    property BallColor: string read fColor write fColor;
    property BallPoints: integer read fPoints write fPoints;
    property Index: integer read fIndex write fIndex;
    property Image: TImage read fImage write fImage;
  end;

implementation

constructor TBubbleStone.Create;
begin
  inherited Create(AOwner);

  Height := 20;
  Width := 20;

  fImage := TImage.Create(AOwner);
  fImage.Parent := Self;
  fImage.Align := alClient;
end;

destructor TBubbleStone.Destroy;
begin
  inherited;
end;

procedure TBubbleStone.Initialize(BallID: integer);
const Colors: array[0..4] of string  = ('Blue','Red','Green','White','Orange');
      Points: array[0..4] of integer = (    80,  200,    150,    300,     500);

begin
  fImage.Picture.LoadFromFile('BITMAPS\Ball'+IntToStr(BallID)+'.bmp');
  fColor := Colors[BallID];
  fForm := 'Circle';
  fPoints := Points[BallID];
end;


und so will ich das OnClickEvent zuweisen

ausblenden volle Höhe 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:
procedure TFormMain.BubbleStoneClick(Sender: TObject);
begin
  if (Sender is TBubbleStone) then begin
     ShowMessage('You clicked on Stone '+IntToStr((Sender as TBubbleStone).Index));
  end;
end;

procedure TFormMain.CreateTable(Sender: TObject);
var i,l,t: integer;
begin
    Randomize;
    RandSeed := Random(100);

    t := 0;
    l:= 0;
    for i:= 1 to BUBBLEMAX do begin
       Stone[i] := TBubbleStone.Create(FormMain);
       Stone[i].Parent := FormMain;
       Stone[i].Name := 'BubbleStone'+IntToStr(i);
       Stone[i].Top := 60 + t*20;
       Stone[i].Left := 220 + l*20;
       Stone[i].Initialize(Random(5));
       Stone[i].Index := i;
       Stone[i].Image.OnClick := BubbleStoneClick;
       Stone[i].OnClick := BubbleStoneClick;
       inc(l);
       if i mod 10 = 0 then begin
         l := 0;
         inc(t);
       end;
       Application.ProcessMessages;
    end;
end;


Allerdings bekomme ich keine Reaktion, wenn ich das Programm laufen lasse.

Warum nicht? :cry:

_________________
Life is, what some people call a mystery. To me life's just a lesson, you're learning when you're through. So why do we try to understand?
CenBells
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1547

Win 7
Delphi XE5 Pro
BeitragVerfasst: Mi 30.10.02 22:33 
hallo du benutzt den aufruf
fImage.align := alClient;

du wartest aber in deiner komponente auf clicks, die auf dein panel erfolgen.

also musst du die property
onclick
deiner kompo mit ner eigenen read und write prozedur versehen, wo du das onclick des panels dann dem des images zuweist.
Dann klappt es auch mit dem nachbarn

Gruß
Ken


Sorry, habe nicht gesehen, daß du das ja eh schon machst....
aogwaba
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 109



BeitragVerfasst: Mi 30.10.02 23:04 
Hi!
Das Image verdeckt das Panel, deshalb kommt das ClickEvent nicht an.
Stone[i].image.OnClick := BubbleStoneClick;

cu
waba
Mike_C Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 207

Win XP
D7 Enterprise
BeitragVerfasst: Do 31.10.02 12:37 
aogwaba hat folgendes geschrieben:

Das Image verdeckt das Panel, deshalb kommt das ClickEvent nicht an.
Stone[i].image.OnClick := BubbleStoneClick;


Darauf bin ich auch schon gekommen, aber das sollte
Stone[i].image.OnClick := BubbleStoneClick;
ja lösen.
Warum funzt das dann trotzdem nicht?

_________________
Life is, what some people call a mystery. To me life's just a lesson, you're learning when you're through. So why do we try to understand?
iKilledKenny
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 8

Win XP
D5 Prof, C# Express 2005
BeitragVerfasst: Do 31.10.02 14:25 
also ohne dass ich es probiert hätte, würde ich folgendes sagen:

wenn du im onclick abfragen würdest
ausblenden Quelltext
1:
2:
3:
4:
5:
if (Sender is TImage) then 
  begin 
     s := IntToStr (TBubbleStone (TImage(Sender).Parent).Index);
     ShowMessage('You clicked on Stone '+s);
  end;


müsste das gehen ...
Mike_C Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 207

Win XP
D7 Enterprise
BeitragVerfasst: Do 31.10.02 14:41 
iKilledKenny hat folgendes geschrieben:
also ohne dass ich es probiert hätte, würde ich folgendes sagen:

wenn du im onclick abfragen würdest
ausblenden Quelltext
1:
2:
3:
4:
5:
if (Sender is TImage) then 
  begin 
     s := IntToStr (TBubbleStone (TImage(Sender).Parent).Index);
     ShowMessage('You clicked on Stone '+s);
  end;


müsste das gehen ...

-------------------------------------------------------------------------------------
Thx alot iKilledKenny!
Logisch, dass es darüber funktionieren muss! Tut's auch!
Macht ja irgendwie keinen Sinn ein TPanelClick abzufangen, wenn ich ein TImageClick abfangen muss!

Thx!

_________________
Life is, what some people call a mystery. To me life's just a lesson, you're learning when you're through. So why do we try to understand?