Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Anfängerfrage Objekte (https://www.delphipraxis.net/123917-anfaengerfrage-objekte.html)

Badyast 11. Nov 2008 08:35


Anfängerfrage Objekte
 
Hallo zusammen,

da ich früher ausschließlich ohne Objekte programmiert habe, schlage ich mich mit der OOP ganz schön herum.

Ein konkretes Beispiel:

Ich habe in meiner Form 20 Comboboxen. Diese möchte ich bequem ansprechen können. z.B.

Combobox[x].clear;

wobei x eine Integervariable zwischen 0 und 20 sein soll.

Das geht so aber leider nicht :?

Könnt ihr mir einen Stoss in die richtige Richtung geben?

Viele Grüße

Daniel

b1zZ 11. Nov 2008 08:46

Re: Anfängerfrage Objekte
 
Hi
mit FindComponent ist das möglich.

Hier ein Beispiel.

Delphi-Quellcode:
for i:= 0 to 20 do
 (FindComponent('Combobox' + IntToStr(i)) as TComboBox).Clear;

Luckie 11. Nov 2008 09:00

Re: Anfängerfrage Objekte
 
Das hat aber im eigentlichen Sinne nichts mit OOP zu tun.

Badyast 11. Nov 2008 09:02

Re: Anfängerfrage Objekte
 
Danke b1zZ!!

:-D

DeddyH 11. Nov 2008 09:21

Re: Anfängerfrage Objekte
 
FindComponent in einer Schleife ist aber nicht sehr performant. Ich würde es mal so versuchen (aus dem Kopf getippt):
Delphi-Quellcode:
for i := 0 to ComponentCount - 1 do
  if Components[i] is TComboBox then
    TComboBox(Components[i]).Clear;

mr_emre_d 11. Nov 2008 12:32

Re: Anfängerfrage Objekte
 
Falls du es in ein Array packen willst:
Code:
var
  Combobox: Array[0..20] of TCombobox;

Initialisierung ( oncreate ):
for i := 0 to 20 do
begin
  Combobox[i] := TCombobox.Create(Self);
  Combobox[i].Parent := Self; // hab jetzt kein Delphi aber so ca müsste es gehn ...
end;

Zugriff:
combobox[2].Clear;

Badyast 11. Nov 2008 16:39

Re: Anfängerfrage Objekte
 
Da habe ich ja jetzt was zum probieren.
Danke!


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:32 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz