crear eventos en tiempo de ejecucion
Como puedo agregar un evento ONCLICK a una serie de botones que creo en tiempo de ejecucion
Holas, si quieres hacerlo en forma completamente dinamica hazlo asi:
CODIGO:
--------------------------------------------------------------------------------
/****************UNIT1.H**********/
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
void __fastcall MiOnClick(TObject *Sender);
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
--------------------------------------------------------------------------------
y finalmente:
CODIGO:
--------------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TButton *MiBoton = new TButton(this);
MiBoton->Top = 10;
MiBoton->Left = 10;
MiBoton->Parent=this;//Form1
MiBoton->Show();
MiBoton->OnClick = MiOnClick;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MiOnClick(TObject *Sender)
{
ShowMessage("Esta haciendo Click en el boton");
}
//---------------------------------------------------------------------------
--------------------------------------------------------------------------------
Asi asignas en tiempo de ejecucion el evento MiOnClick al OnClick de MiBoton, Espero te sirva .
Saludos
_Viktor
CODIGO:
--------------------------------------------------------------------------------
/****************UNIT1.H**********/
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
void __fastcall MiOnClick(TObject *Sender);
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
--------------------------------------------------------------------------------
y finalmente:
CODIGO:
--------------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TButton *MiBoton = new TButton(this);
MiBoton->Top = 10;
MiBoton->Left = 10;
MiBoton->Parent=this;//Form1
MiBoton->Show();
MiBoton->OnClick = MiOnClick;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MiOnClick(TObject *Sender)
{
ShowMessage("Esta haciendo Click en el boton");
}
//---------------------------------------------------------------------------
--------------------------------------------------------------------------------
Asi asignas en tiempo de ejecucion el evento MiOnClick al OnClick de MiBoton, Espero te sirva .
Saludos
_Viktor
