我要在程序运行时,根据实际情况,动态的添加控件(控件的个数不定),同时,自定义一个函数,和控件的click事件建立委托,我现在问,如何在自定义的函数中得到事件的

  • 主题发起人 zhaochaome
  • 开始时间
Z

zhaochaome

Unregistered / Unconfirmed
GUEST, unregistred user!
我要在程序运行时,根据实际情况,动态的添加控件(控件的个数不定),同时,自定义一个函数,和控件的click事件建立委托,我现在问,如何在自定义的函数中得到事件的触发对象阿.我用的是BCB6,谢谢 ( 积分: 200 )<br />我要在程序运行时,根据实际情况,动态的添加控件(控件的个数不定),同时,自定义一个函数,和控件的click事件建立委托,我现在问,如何在自定义的函数中得到事件的触发对象,根据时间触发源的不同,得到触发源的属性,然后,去做一些工作。问体的代码如下:
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 Button1Click(TObject *Sender);
TButton *ddddddd;
void __fastcall FormClick(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall dddddddClick(TObject *Sender);


private: // User declarations
//void _fastcall butclick(TObject *s);
TButton *Button[];
//MESSAGE void WMCommand(TMessage &amp;Message);
MESSAGE void butclick(TMessage &amp;Message);
//{ShowMessage("dddddddddddd");}
public: // User declarations
__fastcall TForm1(TComponent* Owner);
begin
_MESSAGE_MAP
MESSAGE_HANDLER(WM_COMMAND, TMessage, butclick);
END_MESSAGE_MAP(TForm);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp源码
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
//#include <Unit2.h>
#include "Unit1.h"
#include"command.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TButton *Button[10];
for(int i=0;i<10;i++)
{
Button=new TButton(Form1);
Button->Parent=Form1;//指定父控件的属性
//Button->TabOrder=0;
Button->Name ="Button"+IntToStr(i);
Button->Top=Form1->Top+50*i;
Button->Left =Form1->Left+50*i;
Button->Width =50;
Button->Height =50;
//Button->TabStop =true;
Button->Caption="tt"+IntToStr(i);
Button->Font->Size=12;
Button->Visible =true;
//Button->OnClick=butclick;
//delete Button[0];
//ShowMessage(Button->Caption);
}
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
void __fastcall TForm1::FormClick(TObject *Sender)
{
//TButton *Button[10];
//int i=0 ;
//for(int i=0;i<10;i++)
// Button->Visible=true;

}
//---------------------------------------------------------------------------
//void _fastcall TForm1::butclick(TObject *s)//自定义的函数
void TForm1::butclick(TMessage &amp;Message)//自定义的函数
{
//if(Form1->Button0->CanFoucs)
MessageBox(NULL,"提示","tishi",MB_OK);
//AnsiString str =s->ClassName().operator AnsiString().SubString(3,1);
//ShowMessage(s->ClassName());
//ShowMessage(IntToStr(s->Free()));
//this-
//ShowMessage( );
// s->GetInterfaceEntry()
//switch (str.ToInt())
//{
//case 1 :
// Form2 =new TForm2(this);
// if(Form2->ShowModal()==mrOk)
// break;
//case 2 :
// delete Form2;
//}
//int i=0;
//while(i>10)
//{
//}
//for(int j=0;j<10;j++)
//{ //dynamic_cast<TButton*>(Components[j]);
// //if (Button[j]->CanFocus())
// ShowMessage(Button[j]->Caption+"lllll");
// ShowMessage(dynamic_cast<TButton*>(Components[j])->Caption);
// }
}


//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
//Form1->CanFocus()
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

void __fastcall TForm1::dddddddClick(TObject *Sender)
{
ShowMessage("lllllll");

}
//
 
我要在程序运行时,根据实际情况,动态的添加控件(控件的个数不定),同时,自定义一个函数,和控件的click事件建立委托,我现在问,如何在自定义的函数中得到事件的触发对象,根据时间触发源的不同,得到触发源的属性,然后,去做一些工作。问体的代码如下:
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 Button1Click(TObject *Sender);
TButton *ddddddd;
void __fastcall FormClick(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall dddddddClick(TObject *Sender);


private: // User declarations
//void _fastcall butclick(TObject *s);
TButton *Button[];
//MESSAGE void WMCommand(TMessage &amp;Message);
MESSAGE void butclick(TMessage &amp;Message);
//{ShowMessage("dddddddddddd");}
public: // User declarations
__fastcall TForm1(TComponent* Owner);
begin
_MESSAGE_MAP
MESSAGE_HANDLER(WM_COMMAND, TMessage, butclick);
END_MESSAGE_MAP(TForm);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp源码
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
//#include <Unit2.h>
#include "Unit1.h"
#include"command.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TButton *Button[10];
for(int i=0;i<10;i++)
{
Button=new TButton(Form1);
Button->Parent=Form1;//指定父控件的属性
//Button->TabOrder=0;
Button->Name ="Button"+IntToStr(i);
Button->Top=Form1->Top+50*i;
Button->Left =Form1->Left+50*i;
Button->Width =50;
Button->Height =50;
//Button->TabStop =true;
Button->Caption="tt"+IntToStr(i);
Button->Font->Size=12;
Button->Visible =true;
//Button->OnClick=butclick;
//delete Button[0];
//ShowMessage(Button->Caption);
}
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
void __fastcall TForm1::FormClick(TObject *Sender)
{
//TButton *Button[10];
//int i=0 ;
//for(int i=0;i<10;i++)
// Button->Visible=true;

}
//---------------------------------------------------------------------------
//void _fastcall TForm1::butclick(TObject *s)//自定义的函数
void TForm1::butclick(TMessage &amp;Message)//自定义的函数
{
//if(Form1->Button0->CanFoucs)
MessageBox(NULL,"提示","tishi",MB_OK);
//AnsiString str =s->ClassName().operator AnsiString().SubString(3,1);
//ShowMessage(s->ClassName());
//ShowMessage(IntToStr(s->Free()));
//this-
//ShowMessage( );
// s->GetInterfaceEntry()
//switch (str.ToInt())
//{
//case 1 :
// Form2 =new TForm2(this);
// if(Form2->ShowModal()==mrOk)
// break;
//case 2 :
// delete Form2;
//}
//int i=0;
//while(i>10)
//{
//}
//for(int j=0;j<10;j++)
//{ //dynamic_cast<TButton*>(Components[j]);
// //if (Button[j]->CanFocus())
// ShowMessage(Button[j]->Caption+"lllll");
// ShowMessage(dynamic_cast<TButton*>(Components[j])->Caption);
// }
}


//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
//Form1->CanFocus()
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

void __fastcall TForm1::dddddddClick(TObject *Sender)
{
ShowMessage("lllllll");

}
//
 
为什么只有看得,没有给出答案的???是不是有一点菜了??
 
button的onclick事件的格式是
void __fastcall function(TObject *Sender)
其中的sender就是触发事件的按钮,这个是按钮自己实现的,那段代码大概是:
...
if assigned(FOnclick){ FOnclick(this);}
...
你在事件中显示一下sender的名称就知道了
 
用传入的TObject *Sender的控件名字或tag就可以识别.
 
if(((TButton *)Sender)->Name=="..."){
...
}
 
谢谢各位大虾,dawnsong老兄的答案很是清晰一看就毛塞顿开阿,yeskert1的答案有所帮助,深表感谢,而ysws的答案我没有看懂,加上30分,表示谢意。如果有更好的方法请在msn上探讨,我的msn是zhaochaome@hotmail.com。再次表示感谢
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
593
import
I
顶部