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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of Buttons

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello all ,

I asked about how to do an array of labels and cyprus106
answerd me . Now I need to know how can i make an array of buttons (I think it's exactly equal an array of labels) , but I also need to know how can i put code on the OnClick of one of the array buttons.

TIA

Thor
 
Try the following - it works for me. It is from c++Builder 5 standard.

Good Luck - Rod



//Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormCreate(TObject *Sender);
private: // User declarations
void __fastcall p_button_0OnClick(TObject *Sender);
void __fastcall p_button_1OnClick(TObject *Sender);
public: // User declarations
__fastcall TForm1(TComponent* Owner);

};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

//Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include &quot;Unit1.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
TButton *buttons[5];
TButton *p_button_0 = new TButton(Form1);
TButton *p_button_1 = new TButton(Form1);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormCreate(TObject *Sender)
{

buttons[0]=p_button_0;
buttons[1]=p_button_1;

buttons[0]->OnClick = p_button_0OnClick;
buttons[1]->OnClick = p_button_1OnClick;
Form1->InsertControl(buttons[0]);
Form1->InsertControl(buttons[1]);
buttons[1]->Left=200;
buttons[1]->Top=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::p_button_0OnClick(TObject *Sender)
{
// do something...
}

void __fastcall TForm1::p_button_1OnClick(TObject *Sender)
{
// do something...
}

Rod
 
all you have to do is make it exactly like the label array and then when you want an OnClick do:

btn->OnClick = MyButtonOnClick;

Dont put in Parenthesis when assigning. just write it like that and that's all there is to it. Make sure to put in the TObject *Sender and Only TObject *Sender in the MyButtonOnClick Parameters.
[i.e. MyButtonOnClick(TObject *Sender)]

Just in case...
Cyprus
 
Oops, I hate that!. Here, let's try this one again...


all you have to do is make it exactly like the label array and then when you want an OnClick do:

btn[ i ]->OnClick = MyButtonOnClick;

Dont put in Parenthesis when assigning. just write it like that and that's all there is to it. Make sure to put in the TObject *Sender and Only TObject *Sender in the MyButtonOnClick Parameters.
[i.e. MyButtonOnClick(TObject *Sender)]

Just in case...

Cyprus
Cyprus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top