Autor Beitrag
pyto
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Mo 05.04.10 18:59 
Hallo,

Ich bin am verzweifeln. Niemand hatte scheinbar jemals dieses Problem. Internet ist leer.

Angenommen ich habe 13567289 Buttons in meiner Form und Jeder Button hat einen Namen der sich aus 'Button' und seiner Nummer zusammensetzt, und ich will nun mit einer For-Schleife einen Button ansprechen der die Nummer von i hat (I ist die schleifenvariable) , und ich ja nicht für jedes i schreiben kann:

ausblenden Delphi-Quelltext
1:
if i= [1,2,3,usw] then button[1,2,3,usw].irgendwas:=irgendwas;					


(bei 13567289 Buttons wäre das sehr viel arbeit), suche ich eine syntax im format

ausblenden Quelltext
1:
Nmensteil1 + Namesnteil 2 + Variable + Variable					


oder so ähnlich ...

Würde mich sehr freuen wenn jemand so einen code hat.

falls jemand meint das das nicht geht, so poste er das bitte auch hier... ich warte sonst ja ewig :? :) :roll:

Vielen Dank
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8549
Erhaltene Danke: 478

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mo 05.04.10 19:02 
Hallo und :welcome: in der Entwickler-Ecke,

Schau dir mal FindComponent an. Damit sollte das in einer Schleife gehen. :D

_________________
We are, we were and will not be.
pyto Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Di 06.04.10 08:14 
Danke für den tipp,
ich probiers mal... 8) :idea:

_________________
P.S.: Ich nutze Delphi 5 und 7
pyto Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Mi 07.04.10 09:22 
user profile iconpyto hat folgendes geschrieben Zum zitierten Posting springen:
Danke für den tipp,
ich probiers mal... 8) :idea:


Hat geklappt... :!: :idea: :lol: :wink:

_________________
P.S.: Ich nutze Delphi 5 und 7
Xion
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
EE-Maler
Beiträge: 1952
Erhaltene Danke: 128

Windows XP
Delphi (2005, SmartInspect), SQL, Lua, Java (Eclipse), C++ (Visual Studio 2010, Qt Creator), Python (Blender), Prolog (SWIProlog), Haskell (ghci)
BeitragVerfasst: Mi 07.04.10 09:54 
Du könntest die Buttons auch als Array machen, was wohl auch beim Form erstellen einfacher wäre:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
var
Buttons: array of TButton;
...
SetLength(Buttons,1234567); //=1234567 Buttons
for A:= 0 to High(Buttons) do
  begin
    Buttons[A]:=TButton.Create(Form1);
    Buttons[A].Parent:=Form1;
    Buttons[A].Left:=A*10;
    Buttons[A].Top:=0;
    Buttons[A].Width:=10;
    Buttons[A].Height:=10;
    Buttons[A].Tag:=A; //damit kannst du dann auch vom Button rausfinden, welches Element des Arrays er ist
    Buttons[A].Caption:=inttostr(A);
    Buttons[A].OnClick:=AllButtonsOnClick;
  end;


Das ansprechen wäre dann sehr einfach
ausblenden Delphi-Quelltext
1:
Buttons[4].Caption:='x';					

oder
ausblenden Delphi-Quelltext
1:
2:
for A:= 0 to 25 do
  Buttons[4].Caption:='y';


und wenn du z.B. den Sender aus ner OnClick kriegst:
ausblenden Delphi-Quelltext
1:
2:
if (Sender is TButton) then
  id:=TButton(Sender).Tag

_________________
a broken heart is like a broken window - it'll never heal
In einem gut regierten Land ist Armut eine Schande, in einem schlecht regierten Reichtum. (Konfuzius)
Tilo
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1098
Erhaltene Danke: 13

Win7 geg. WInXP oder sogar Win98
Rad2007
BeitragVerfasst: Mi 07.04.10 13:07 
user profile iconXion hat folgendes geschrieben Zum zitierten Posting springen:
oder
ausblenden Delphi-Quelltext
1:
2:
for A:= 0 to 25 do
  Buttons[4].Caption:='y';



Da war sicherlich folgendes gemeint:
ausblenden Delphi-Quelltext
1:
2:
for A:= 0 to 25 do
  Buttons[A].Caption:='y';


Entschuldigung für die Besserwisserei,
Ich freue mich wenn ich auch mal was konstruktives so kurz nach dem Mittag posten kann.
Tropby
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 71
Erhaltene Danke: 4

Vista, Win XP, Win 89
Turbo Delphi Ex.
BeitragVerfasst: Mi 07.04.10 13:38 
ausblenden Delphi-Quelltext
1:
Nmensteil1 + Namesnteil 2 + Variable + Variable					


Wenn du das so in der Art machen möchtest hätte ich noch folgende lösung:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function TForm1.GetComponentByName(Name : String) : TComponent;
var
  I : Integer;
begin
  Result := nil;
  for I := 0 to Form1.ComponentCount - 1 do
    if Form1.Components[I].Name = Name then
      Result := Form1.Components[I];
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TPanel(GetComponentByName(Edit1.Text)).Caption := '####';
end;

_________________
Tropby
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 07.04.10 13:49 
Moin!

Dann aber bitte Resourcen-schonender und etwas allgemeiner: ;)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
function GetComponentByName(AForm: TForm; const AName: String): TComponent;
  var
    i: Integer;
begin
  for i := 0 to AForm.ComponentCount -1 do begin
    Result := AForm.Components[i];
    if (Result.Name = AName) then
      Exit;
  end;
  Result := NIL;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TPanel(GetComponentByName(Self,Edit1.Text)).Caption := '####';
end;
cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.