Topic: point components in loop

Hi,

is there any way to point components in a loop?
I just have about 50 panels and want to go through them one by one in a loop instead of writing of 50 similar line of codes! something like this:

   for i := 1 to 5 do
  begin
        Main.Panel(i).Caption := 'Panel number is: ' + IntToStr(i);

  end;

Many Thanks

Re: point components in loop

Hi Sonixax,
Perhaps try it like this (please see attached - click on the 'LOOP' button to run it).
Derek.

Post's attachments

Attachment icon panelcount.zip 337.32 kb, 242 downloads since 2022-01-23 

Re: point components in loop

Hi Sonixax and Derek!

Small clarification: the name of the panel class used in the MVDB is TdbPanel.

procedure frmForm_btnButton_OnClick (Sender: TObject; var Cancel: boolean);
var  
  i: integer;
begin
  for i := 0 to frmForm.ComponentCount-1 do
    if frmForm.components[i] is TdbPanel then 
      TdbPanel(frmForm.components[i]).caption := 'Component number is: ' + IntToStr(i);
end;

The code Derek suggested will also work, since TdbPanel is the heir of TPanel. However, to access all the properties of the MVDB panel, you need to use a cast to the TdbPanel class.

Визуальное программирование: блог и телеграм-канал.

Re: point components in loop

Many many Thanks,
Is it possible to have such a functionality for events too?
For example onclick?

Many many Thanks again.

Re: point components in loop

sonixax wrote:

Many many Thanks,
Is it possible to have such a functionality for events too?
For example onclick?

Many many Thanks again.

The name of the event handler is in the db<event> property, for example onClick:

procedure frmForm_btnButton_OnClick (Sender: TObject; var Cancel: boolean);
var  
  i: integer;
begin
  for i := 0 to frmForm.ComponentCount-1 do
    if frmForm.components[i] is TdbPanel then 
      TdbPanel(frmForm.components[i]).caption := TdbPanel(frmForm.components[i]).dbOnClick;
end;
Визуальное программирование: блог и телеграм-канал.

Re: point components in loop

k245 wrote:
sonixax wrote:

Many many Thanks,
Is it possible to have such a functionality for events too?
For example onclick?

Many many Thanks again.

The name of the event handler is in the db<event> property, for example onClick:

procedure frmForm_btnButton_OnClick (Sender: TObject; var Cancel: boolean);
var  
  i: integer;
begin
  for i := 0 to frmForm.ComponentCount-1 do
    if frmForm.components[i] is TdbPanel then 
      TdbPanel(frmForm.components[i]).caption := TdbPanel(frmForm.components[i]).dbOnClick;
end;

Many thanks for the answer,
But I need something else.
let me explain:

I have around 200 panels, and I want instead of 200 times code like this one:

procedure Main_Panel10_OnClick (Sender: TObject);
begin

end;

only have one loop.
every single panel (button) should linked to a record and open a form to show that record.

I can send the project if you need it.
many many thanks

7 (edited by sibprogsistem 2022-01-31 04:34:09)

Re: point components in loop

ссылайте все компоненты на одно имя процедуры..

а вообще уточните, что Вы хотите сделать..


   

 
refer all components to the same procedure name..

and in general, specify what you want to do.

Re: point components in loop

sibprogsistem wrote:

ссылайте все компоненты на одно имя процедуры..
а вообще уточните, что Вы хотите сделать..

refer all components to the same procedure name..
and in general, specify what you want to do.

sibprogsistem, он (она) хочет, чтобы ты готовый код написал )))
Задача несложная: на форме 200 панелей, каждая панель связана с записью в таблице (как именно связана, автор не уточняет), при нажатии на панель должна открываться форма редактирования для данной записи. Например, при создании панелей их можно назвать panShowRecordButton_#, где # - ID записи, которую нужно показать.

ИМХО, информации для реализации этого функционала предостаточно, но автор топика хочет готовый код. Мой ответ здесь: http://myvisualdatabase.com/forum/viewtopic.php?id=6262
Но наверняка есть и другие варианты   )))

Визуальное программирование: блог и телеграм-канал.

Re: point components in loop

Hello Everyone,
Sonixax,
Perhaps you could attach your project (ideally with some data in it) which would be helpful to demonstrate your problem .  As K245 has suggested, there may be different ways to achieve what you want to do.  200 panels (and, if I follow the discussion correctly,  each panel linked to a record) seems excessive and does not appear, at least initially, to be taking a relational database approach.
Derek.

Re: point components in loop

derek wrote:

Hello Everyone,
Sonixax,
Perhaps you could attach your project (ideally with some data in it) which would be helpful to demonstrate your problem .  As K245 has suggested, there may be different ways to achieve what you want to do.  200 panels (and, if I follow the discussion correctly,  each panel linked to a record) seems excessive and does not appear, at least initially, to be taking a relational database approach.
Derek.

Sure,
Here it is,
Still working on it.
There is some rooms, every panel represent a room.
I have a form which is called AddBox, I just want on click on every single panel AddBox opens to show the room record. more details will add later currently just open the form to show it.
I just starting to replace buttons with panels cause cannot change buttons color.
if possible to draw panels with a loop to, it will be amazing cause maybe in future we are going to change the map so we have to re arrenge everything from source again. of course it will be easier to add, remove or position them within the final product.

many many thanks everyone

Post's attachments

Attachment icon AU.zip 476.09 kb, 225 downloads since 2022-01-31 

11 (edited by derek 2022-02-06 17:24:17)

Re: point components in loop

Hi Sonixax,
I had a quick look at your program.
I didn't want to change your script and forms as they are quite complicated so I just created a new application which is a scaled-down version of yours.
You simply click on any 'box' and then go and add / amend the details and color-code 'boxes' that are free or available.  The script loops through however many components you've got on the form so you don't actually need to make any changes to the script even if you are adding more (or deleting any) 'boxes' .
It might not be everything you need but hopefully you can use some of it as a basis to move forward.
Derek.

UPDATE:
Made a couple of changes to the attachment so you can search by customer (which may be something you need) or storage unit.

Post's attachments

Attachment icon storage units.zip 612.6 kb, 240 downloads since 2022-02-06