Autor Beitrag
mexx
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 14.02.06 09:43 
Hallo,

ich möchte den Namen der Komonenten zur Laufzeit ansprechen. So in etwa!


IST:
ausblenden Delphi-Quelltext
1:
 Image4.Picture.LoadFromFile					


SOLL:
ausblenden Delphi-Quelltext
1:
 Image+Variable.Picture.LoadFromFile					


Geht das irgendwie?
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Di 14.02.06 09:44 
Jep. Das geht. Stichwort ist FindComponent.

_________________
We are, we were and will not be.
mexx Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 14.02.06 10:04 
Kann ich diese Methode denn auch in Funktionen verwenden oder ist dafür eine Besondere deklaration nötig?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
function KarteLegen(Karte: Integer):integer;
begin
   a := Random(12);
   case a of
 0: begin
      with TImage(FindComponent(NamePrefix + IntToStr(Karte))) do
       begin
        Picture.LoadFromFile(Pfad + IntToStr(2) + '.bmp');
        Result := 2;
       end;

...


Er kennt FindComponent und NamePrefix nicht!
Danny87
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 688

Windows 10 Pro 64bit
Sprachen: HTML, PHP, JavaScript, Delphi || IDE: RAD Studio 10.1 Berlin Starter, WeBuilder
BeitragVerfasst: Di 14.02.06 10:11 
schau mal: www.delphi-forum.de/...55859&highlight=

Was hast du für ein delphi, das FindComponent nicht kennt?


gruss daniel
mexx Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 14.02.06 10:16 
Delphi 7 Enterprise

Ich denke nicht, dass es an Delphi liegt. Ich denke, dass ich meine Function nicht korrekt definiert habe. An welche stelle über der Implementation muss meine Function definiert werden, damit die darin enthaltenden Komponenten automatisch auf der Form gefunden werden. Dann werden auch die Units genutzt und das FindComponent erkannt. Liege ich mit meinem Verständnis richtg. Wenn ja muss ich eingestehen, dass ich dahingehend eine große Wissenslücke aufweise.
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Di 14.02.06 10:21 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
function TForm1.KarteLegen(Karte: Integer):integer; // Als Methode der Form definieren!
begin
   a := Random(12);
   case a of
 0: begin
      with TImage(FindComponent(NamePrefix + IntToStr(Karte))) do
       begin
        Picture.LoadFromFile(Pfad + IntToStr(2) + '.bmp');
        Result := 2;
       end;

...
mexx Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 14.02.06 10:25 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
...

  private

    { Private-Deklarationen }
  public

    { Public-Deklarationen }
  end;
function KarteLegen(Karte: Integer):integer;

var
  Form1: TForm1;



...


Keine Veränderung! Die Fehlermeldung dee unbekannten FindComponent und NamePrefix beiben!
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Di 14.02.06 10:30 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
  private
    { Private-Deklarationen }
    function KarteLegen(Karte: Integer):integer; // Hier ist das besser.
  public

    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
raziel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2453

Arch Linux
JS (WebStorm), C#, C++/CLI, C++ (VS2013)
BeitragVerfasst: Di 14.02.06 10:31 
FindComponent ist ja auch eine Methode der Klasse TForm, also so:
ausblenden Delphi-Quelltext
1:
  Form1.FindComponent(...);					

sollte es gehen.

Gruß,
raziel

_________________
JSXGraph
mexx Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 14.02.06 10:36 
Es funktioniert, danke!
mexx Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 14.02.06 10:41 
Doch nicht so erfolgreich das ganze. Laut Einzelanweisung gibt es hier einen Zugriffsverletzung.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
      with TImage(Form1.FindComponent(IntToStr(Karte))) do
       begin
        Picture.LoadFromFile(Pfad + IntToStr(6) + '.bmp');//Hier die erscheint die Zugriffsverletzung
        Result := 6;
       end;


Die Pfadangabe ich aber korrekt! Es muss etwas mit dem Bilden des Komponentennamen zu tun haben.
Kroko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1284

W98 W2k WXP
Turbo D
BeitragVerfasst: Di 14.02.06 10:50 
geändert in
user profile iconmexx hat folgendes geschrieben:
Doch nicht so erfolgreich das ganze. Laut Einzelanweisung gibt es hier einen Zugriffsverletzung.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
      with TImage(Form1.FindComponent('Image'+IntToStr(Karte))) do //Besser oder wie sie heißen
       begin
        Picture.LoadFromFile(Pfad + IntToStr(6) + '.bmp');//Hier die erscheint die Zugriffsverletzung
        Result := 6;
       end;


Die Pfadangabe ich aber korrekt! Es muss etwas mit dem Bilden des Komponentennamen zu tun haben.

_________________
Die F1-Taste steht nicht unter Naturschutz und darf somit regelmäßig und oft benutzt werden! oder Wer lesen kann, ist klar im Vorteil!
mexx Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 14.02.06 10:56 
Selber Fehlermeldung bei dieser Source.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
      with TImage(Form1.FindComponent('Image' + IntToStr(Karte))) do
       begin
        Picture.LoadFromFile(Pfad + 'D' + '.bmp');
        Result := 10;
       end;


Ergibt den dies ...

ausblenden Delphi-Quelltext
1:
with TImage(Form1.FindComponent('Image' + IntToStr(Karte))) do					


... auch das

ausblenden Delphi-Quelltext
1:
with Image1 do //1 nur als Beispiel					


Kann man dies irgendwie überprüfen?
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Di 14.02.06 12:35 
Hallo,
wenn Karte eine Integervariable ist, sie den Wert 1 hat und die Komponente Image1 vorhanden ist,
ergibt das with Image1 do.
Überprüfen kannst Du das mit:
ausblenden Delphi-Quelltext
1:
if Form1.FindComponent('Image' + IntToStr(Karte)) = Image1 then					

wenn man den Typ der Komponente überprüfen will, geht das mit dem is-Operator:
ausblenden Delphi-Quelltext
1:
if Form1.FindComponent('Image' + IntToStr(Karte)) is TImage then					

Existiert die Komponente nicht, wird False zurückgegeben.

Am besten ist Du legst Dir dafür eine Variable an:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
var aObj : TObject;
begin
  aObj := Form1.FindComponent('Image'+IntToStr(Karte));
  if aObj is TImage then//True = wurde gefunden und ist TImage > Typcast auf TImage 
    with TImage(aObj) do
      begin  
      Picture.LoadFromFile(Pfad + IntToStr(6) + '.bmp');
      Result := 6;  
      end;

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
mexx Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 14.02.06 13:04 
Ich komme nicht weiter. Es ist definitiv vom Typ TImage. Und offentsichtlich läd er sogar das Bild. Jedoch nicht immer. Es ist nahezu verwirrend. Manchmal läd er das Bild trotz Fehlermeldung und manchmal nicht.
mexx Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 14.02.06 13:13 
Wenn ich die Source in eine Schaltfläche packe funktioniert Sie. In der Funktion jedoch nicht.

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:
...

  private

    { Private-Deklarationen }
  public

    { Public-Deklarationen }
  end;
function KarteLegen(Karte: Integer):integer;

var
  Form1: TForm1;

...

function KarteLegen(Karte: Integer):integer;
begin
Randomize;
   a := Random(12);
   case a of
 0: begin
      with TImage(Form1.FindComponent('Image' + IntToStr(Karte))) do
       begin
        Picture.LoadFromFile(Pfad + '2' + '.bmp');
        Result := 2;
       end;
    end;

...


Moderiert von user profile iconGausi: Delphi-Tags korrigiert.
azrael_7
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 78

Windows Vista
D2005 Pers. [de]
BeitragVerfasst: Di 14.02.06 13:21 
user profile iconmexx hat folgendes geschrieben:
Wenn ich die Source in eine Schaltfläche packe funktioniert Sie. In der Funktion jedoch nicht.

Wie user profile iconjasocul schon schrieb:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
...

  private

    { Private-Deklarationen }
  public

    { Public-Deklarationen }
  end;
function KarteLegen(Karte: Integer):integer;

var
  Form1: TForm1;

...

function Form1.KarteLegen(Karte: Integer):integer; 
begin
   ...
end;

_________________
-·- Es gibt Leute die bringen Freude, wohin sie gehen -·-
-·- und es gibt Leute, die bringen Freude, wenn sie gehen -·-
mexx Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1183



BeitragVerfasst: Di 14.02.06 13:27 
[Fehler] Unit1.pas(51): Bezeichner redefiniert: 'Form1'
[Fehler] Unit1.pas(35): Ungenügende Forward- oder External-Deklaration: 'KarteLegen'

unveränderte Source, lediglich azrael_7 Vorschlag umgesetzt
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Di 14.02.06 13:36 
Das muss auch noch gemacht werden. (Wie schon weiter oben geschrieben)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
  private
    { Private-Deklarationen }
    function KarteLegen(Karte: Integer):integer; // Hier ist das besser.
  public

    { Public-Deklarationen }
  end;
//function KarteLegen(Karte: Integer):integer; // und hier löschen.

var
  Form1: TForm1;

Es gibt auch noch die Möglichkeit in der Funktion voll-qualifiziert zu programmieren, aber damit tust du dir keinen Gefallen.
Gewöhne dir am Besten gleich an, objekt-orientiert zu programmieren. Deine Funktion sollte eine Methode von TForm1 sein. Zumal sie funktional eindeutig dazu gehört.
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: Di 14.02.06 13:39 
Da muss doch irgendein anderer fehler sein das Funktioniert ja auch, jetzt mal ohne Rückgabewert.
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:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function KarteLegen(Karte: Integer):Integer; 
var
 i:Integer;
begin
 Randomize;
 i := Random(Karte)+1;
  with TLabel(Form1.FindComponent('Label' + IntToStr(i))) do
   begin
    Visible:=False;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  KarteLegen(6);
end;

end.