![]() |
Findcomponent löst Exception aus???
Hi,
Ich will auf eine Image-Komponente mit Findcomponent zugreifen. Da dies jedoch immer eine Exception ausgelöst hat, hab ich 2 Showmessages an den Anfang gesetzt. Die erste wird angezeigt und dann kommt ne Exception(Ungültige Typenumwandlung) Der Name der Komponente ist im_st1nr1. Stapel[1].Anzahl ist auch 1. Hab ich irgend einen Fehler übersehen?, oder warum klappt das nicht?
Delphi-Quellcode:
Edit: Es kommt Ne Exception(Ungültige Typenumwandlung), nicht zugriffsverletzung
showmessage('s');
showmessage((Findcomponent('im_st'+ InttoStr(1) + 'nr1'+ InttoStr(Stapel[1].Anzahl) ) as TImage).name); |
Re: Findcomponent löst Exception aus???
Moin,
wenn FindComponent die Komponente nicht findet, wird ein nil-Zeiger zurückgegeben. Du solltest also immer prüfen, ob FindComponent auch eine Komponente liefert. also...
Delphi-Quellcode:
Ausserdem solltest du, wenn du schon Text mit einer ShowMessage ausgibst vielleicht den String ausgeben, den du an FindComponent übergibst, dann würdest du nämlich auch bestimmt sehen, dass die Komponente du du finden möchtest nicht existiert.
var Image:TImage;
begin Image:=Findcomponent('im_st'+ InttoStr(1) + 'nr1'+ InttoStr(Stapel[1].Anzahl)) as TImage; if assigned(Image) then showmessage(Image.name); end; MfG Thorsten |
Re: Findcomponent löst Exception aus???
Ja, das könnt ich machen, aber das hilft mir bei meinem problem auch nicht, da die Komponente ja vorhanden ist.
|
Re: Findcomponent löst Exception aus???
Hi Thorben86,
Delphi-Quellcode:
Überprüf mal ob die Kompo überhaupt ein Image ist, also z.B. so:
showmessage((Findcomponent('im_st'+ InttoStr(1) + 'nr1'+ InttoStr(Stapel[1].Anzahl) ) as TImage).name);
Delphi-Quellcode:
Greetz
var
c: TObject; begin c := Findcomponent('im_st'+ InttoStr(1) + 'nr1'+ InttoStr(Stapel[1].Anzahl)); if c is TImage then ShowMessage(TImage(c).Name); end; alcaeus |
Re: Findcomponent löst Exception aus???
Thx @ alcaeus: Ich hab jetzt das Problem gefunden, ich hatte die Images durch Shapes ersetzt :wall: :wall: :wall: :oops:
:dp: |
Re: Findcomponent löst Exception aus???
Aber jetzt klappt es irgendwie nicht, den Namen davon dann umzuändern.
Klappt das generell nicht oder hab ich schon wieder nen Fehler eingebaut?
Delphi-Quellcode:
(Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape).Name := 'im_st'+ InttoStr(Zielstapel) + 'nr' + InttoStr(Stapel[Zielstapel].Anzahl + 1);
|
Re: Findcomponent löst Exception aus???
Hi Thorben86,
sag mal was denn nicht funktioniert. Gibts eine Fehlermeldung? AV? Greetz alcaeus |
Re: Findcomponent löst Exception aus???
Sry, es kommt ne Exception, dass es eine Zugriffsverletzung gibt.
|
Re: Findcomponent löst Exception aus???
Hi Thorben86,
versuch mal, das Ergebnis von FindComponent in einer Variable abzuspeichern und anschließend den Namen zu ändern. Vielleicht gibts Probleme, weil du ja indirekt der Funktion was zuweist. Greetz alcaeus |
Re: Findcomponent löst Exception aus???
Ahmm, könntest du vielleicht mal en beispiel machen, wie ich das mache, weil wenn ich den dateinamen auf ner Variable hab, kann ich den zwwar auf der variable ändern, aber wie änder ich den dann bei der Kompo
|
Re: Findcomponent löst Exception aus???
Hi Thorben86,
du sollst das Ergebnis von FindComponent abspeichern, und nicht den Namen des Ergebnisses :zwinker: Als Beispiel:
Delphi-Quellcode:
Greetz
var
c: TComponent; begin c := TComponent(Findcomponent('im_st'+ InttoStr(1) + 'nr1'+ InttoStr(Stapel[1].Anzahl))); c.Name := ... end; alcaeus |
Re: Findcomponent löst Exception aus???
Aso, dabei kommt aber glaub ich die gleiche Exception raus(Es kommt auf jedenfall auc hne Zugriffsverletzung raus)
|
Re: Findcomponent löst Exception aus???
Hmm, also bei einem durchgang klappt es irgendwie jetzt auf einmal. Bei dem 2. durchgang kommt jetz an der anderen stelle ne Exception :gruebel: .
Die Komplette FDunktion:
Delphi-Quellcode:
var Kompo:TComponent;
begin Kompo:=TComponent(Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape); Showmessage(inttostr(2)); //Das wird noch angezeigt, dann kommt die Exct (Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape).Left:= Stapel[Zielstapel].left; (Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape).Top:= Stapel[Zielstapel].Top - (15 * (Anzahl+1)) ; inc(Stapel[Startstapel].Anzahl,-1); inc(Stapel[Zielstapel].Anzahl); Kompo.Name:='im_st'+ InttoStr(Zielstapel) + 'nr' + InttoStr(Stapel[Zielstapel].Anzahl + 1); end; |
Re: Findcomponent löst Exception aus???
es wurde schon zig mal geschrieben das du das Ergebnis von TComponent erstmal auf eine Variable zuweisen sollst und diese dann auf ungleich "nil" überprüfen sollst. Wenn dir FindComponent bei
Delphi-Quellcode:
zurückgibt weil die Komponente mit dem Namen nicht existiert ist klar das dann eine Exception kommt.
(Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape)
richtiger ist
Delphi-Quellcode:
Warum schreibst du überhaupt
var LShape: TShape;
begin LShape := Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)); if LShape <> nil then begin LShape.Left:=Stapel[Zielstapel].left; end;
Delphi-Quellcode:
Wenn du den Wert den du auf Kompo zugewiesen hast dann nicht nutzt? Zu dem Ist es überflüssig das ganze nach TShape zu casten wenn du es dann der Variablen "kompo" zuweist die vom Typ "TComponent" ist. Besser wäre die Variable "Kompo" als "TShape" zu declarieren und dann mit "Kompo" weiterzuarbeiten und nicht immer wieder "FindComponent" zu nutzen da dies auch ein Performanceeinbruch ist.
Kompo:=TComponent(Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape);
|
Re: Findcomponent löst Exception aus???
Sry, dat ich nit früher antworten konnt, ming I-net war seit 10 aus :? .
Hatte das eben auch schon geänert, aber das bringt mir nichts, da es ne Exception gibt, wenn ich die Procedure zum 2. mal aufrufe(die Wird in ner Rekursiven Procedure mit aufgerufen) und auf die Variable zugreife(also z.b. Kompo.Left) Edit: Beim 2. durchlauf wird Kompo irgendwie nil, nur wieso versteh ich nicht, da dürfte doch eigentlich kein unterschied sein. Jetzt sieht der Text so aus (beim 2. Durchlauf ist Kompo=nil)
Delphi-Quellcode:
var Kompo:TShape;
begin Kompo:=(Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape); if(Kompo=nil)then Showmessage('nil'); Showmessage(inttostr(3)); Kompo.Left:= Stapel[Zielstapel].left - (Kompo.Width div 2);// - (Findcomponent('im_st'+ InttoStr(Startstapel) + 'nr' + InttoStr(Stapel[Startstapel].Anzahl)) as TShape).width; Kompo.Top:= Stapel[Zielstapel].Top - (15 * (Anzahl+1)) ; inc(Stapel[Startstapel].Anzahl,-1); inc(Stapel[Zielstapel].Anzahl); Kompo.Name:='im_st'+ InttoStr(Zielstapel) + 'nr' + InttoStr(Stapel[Zielstapel].Anzahl); Showmessage(Kompo.name); |
Re: Findcomponent löst Exception aus???
Da gibt's IMHO nur 2 Sachen:
|
Re: Findcomponent löst Exception aus???
Ahm, bei Punkt 2 wüsst ich nicht, was ich machen müsste, aber es klappt jetzt auf jedenfall, irgendwie hat der vorher die Anzahl falsch eingelesen.
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:15 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz