Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Intercepting Click from a Button within an array?????

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,
I need to create an array of Buttons. One for each record found in a file. How can I intercept events from components that are within an array? Example, How do I know when and which of those Buttons got clicked without using TLIST?

I thought it would be easy as in VB, but I'm stuck with this since 6 hours and I can't find documentation about behavior of component-arrays.

If it will help, I have a litle demo-project about my dificulty. My e-mail is: gtekes@velonet.com.br

Thanks a lot and greetings from Brazil
Gerhard

 
Could you make use of CheckListBox?

The property checked gives an indication
Code:
for i:=1 to (CheckListBox1.Items.Count - 1) do
        if (CheckListBox1.Checked[i]) then

Regards S. van Els
SAvanEls@cq-link.sr
 
When you create the buttons, presumably in a loop, set the OnClick property of that button to point to your handler:
Code:
var mybutton: TButton;
begin
  for i := 0 to mylistmax do begin
    mybutton := TButton.Create(Self);
    mybutton.Left := 10;
    mybutton.Top := i * 30;
    mybutton.Caption := 'whatever';
    mybutton.Tag := i;
    mybutton.OnClick := mybuttonClick;
  end;
end;
Self is the form that owns this procedure. Using Self in the Create() means that this form owns this button.

Tag is a property of all controls that you can use for whatever you want. Here, we use it to identify which button this is.

Setting the OnClick tells Delphi what to do when the user clicks the button. Of course, we need to create that onclick routine. The easiest way to do this is to create a button on your form called mybutton, double-click it, then fill in the code in the blank procedure Delphi gives you, then delete the mybutton from the form.
Code:
procedure TForm1.mybuttonClick(Sender: TObject);
var i: Integer;
begin
  i := (Sender as TComponent).Tag;
  ShowMessage('You pressed the ' + IntToStr(i) + 'th button.);
end;
I've filled in some sample code.

Delphi is capable of doing everything that VB can do with arrays of controls; but sometimes in Delphi it's not quite so immediately obvious as to how to do it.

Note that I haven't bothered to create an actual array or TList for my buttons to live in -- they're just owned by the form like any other control, and when the form is Release'd, they'll automatically be freed just like any other control.
-- Doug Burbidge mailto:doug@ultrazone.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top