Autor Beitrag
Hendrik
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 324



BeitragVerfasst: Mi 26.09.12 19:45 
Hallo,

ich möchte gerne über eine Procedure einer Komponente eine Procedure für das OnClick-Ereignis zuweisen. Folgende Procedure habe ich geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure setonclick(value:string;cont:TComponent);
 var
 ev:TNotifyevent;
begin
  value:=lowercase(value);

  if value='p1' then
  ev:=buttonp1Click;
  if value='p2' then
  ev:=buttonp2Click;

  if cont is TButton then begin
     (cont as TButton).onclick:=ev;
  end;

  if cont is TMenuitem then begin
    (cont as TMenuitem).OnClick:=ev;
  end;
end;


Leider kommt beim OnClick auf den Button immer eine Zugriffsverletzung. Hat jemand eine Idee?
Nersgatt
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1581
Erhaltene Danke: 279


Delphi 10 Seattle Prof.
BeitragVerfasst: Mi 26.09.12 20:54 
Die Zugriffsverletzung kommt erst, wenn man auf den Button klickt? Dann wäre der Fehler in Buttonp1Click bzw. Buttonp2Click zu suchen und nicht in dem Quellcode, den Du hier gespostet hast.

_________________
Gruß, Jens
Zuerst ignorieren sie dich, dann lachen sie über dich, dann bekämpfen sie dich und dann gewinnst du. (Mahatma Gandhi)
glotzer
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 393
Erhaltene Danke: 49

Win 7
Lazarus
BeitragVerfasst: Mi 26.09.12 22:18 
[quote="user profile iconHendrik"(668133)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure setonclick(value:string;cont:TComponent);
 var
 ev:TNotifyevent;
begin


hast du "ev" keinen Wert zugewiesen? Falls nicht ist es standardmäsig nil, d.h. Zugriffsverletzung.

_________________
ja, ich schreibe grundsätzlich alles klein und meine rechtschreibfehler sind absicht
bummi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1248
Erhaltene Danke: 187

XP - Server 2008R2
D2 - Delphi XE
BeitragVerfasst: Mi 26.09.12 23:28 
nil gäbe keine Zugriffsverletzung, aber Du hast trotzdem Recht, weil ev dann einen beliebigen Stackwert als Adressen darstellen würde, wen value weder p1 noch p2 wären ... und das gäbe ziemlich sicher eine Zugriffsveretzung.

_________________
Das Problem liegt üblicherweise zwischen den Ohren H₂♂
DRY DRY KISS
Hendrik Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 324



BeitragVerfasst: Do 27.09.12 15:24 
Das von euch genannte Problem hatte ich nicht bedacht, das wars. Danke!