请教:RadioButton动态生成后响应事件(如Onclick)如何编写?(100分)

  • 主题发起人 主题发起人 qifenglin
  • 开始时间 开始时间
Q

qifenglin

Unregistered / Unconfirmed
GUEST, unregistred user!
先粘一段代码:
procedure TForm1.MakePeperCard(TestTypeID,TestProbNum,topSpace,TestNo:integer;lblPanel:TPanel);
var
lblTopic:TLabel;
SingleBtn: array of TRadioButton;
//MultiBtn: array of TCheckBox;
//EditBtn: array of TEdit;
i:integer;
begin
case TestTypeID of
1: //单项选择题
begin
setLength(SingleBtn,TestProbNum);
lblTopic:=TLabel.Create(self);
lblTopic.Parent:=lblPanel;
lblTopic.Left:=6;
lblTopic.Top:=3;
for i:=0 to TestProbNum-1 do
begin
SingleBtn:=TRadioButton.Create(self);
SingleBtn.parent:=lblPanel;
lblTopic.Caption:=intToStr(TestNo)+'. ';
SingleBtn.Caption:=chr(65+i);
SingleBtn.Width:=40;
SingleBtn.Height:=17;
SingleBtn.Enabled:=true;
SingleBtn.Checked:=false;
SingleBtn.Left:=30+i*40;
SingleBtn.Top:=3;
SingleBtn.Tag:=i;
SingleBtn.Name:= chr(65+i)+intToStr(TestNo);
end;
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
j,topSpace1:integer;
lblPanel1:TPanel;
begin
topSpace1:=0;
for j:=0 to 19 do
begin
lblPanel1:=TPanel.Create(self);
lblPanel1.Parent:=panel1;
lblPanel1.Left:=8;
lblPanel1.Top:=10+topSpace1;
lblPanel1.Width:=32+40*4;
lblPanel1.Height:=20;
lblPanel1.BevelInner:=bvNone;
lblPanel1.BevelOuter:=bvNone;
//调用过程;
MakePeperCard(1,4,topSpace1,j+1,lblPanel1);
topSpace1:=topSpace1+20;
end;
end;
以上是动态生成RadioButton的过程,并用button1调用。如何编写每一组中的RadioButton响应事件?
其中RadioButton的个数实际是从数据库中取出的,为不确定数。
 
procedure TForm1.Button1Click(Sender: TObject);
begin
RadioButton1.OnClick := RadioButtonClick;
end;

procedure TForm1.RadioButtonClick(Sender: TObject);
begin
showmessage('click');
end;
 
to:影子
这种方法好象不行,我在public中添加了
procedure RadioButtonClick(Sender: TObject);
在下面的事件中调用:
procedure TForm1.Button3Click(Sender: TObject);
var
i:integer;
valueStr:String;
GetResult:TStrings;
begin
valueStr:='';
for i:=0 to ComponentCount-1 do
if Components is TRadioButton then
if TRadioButton(Components).Checked then
begin
valueStr:=valueStr + '"' + TRadioButton(Components).Name + '",';
TRadioButton(Components).OnClick:= RadioButtonClick;
end;
valueStr:=copy(valueStr,0,length(valueStr)-1);
GetResult:=SplitString(valueStr,',');
for i:=0 to GetResult.Count-1 do
begin
//利用Copy分割GetResult,判断其长度,取编号和选择答案向表中写记录;
end;
//showmessage(valueStr);
end;
但是没有反应!
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;

type
TForm1 = class(TForm)
RadioButton1: TRadioButton;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Click1(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Click1(Sender: TObject);
begin
ShowMessage('aaa');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Click1(Sender);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
RadioButton1.OnClick := Click1;
end;

end.

行吗?
 
指定RadioButton是可以的,但是我是在动态生成后,要求单击其中某组中的某个RadioButtom
就可以响应事件。
请高手们将我上面的代码可以直接粘到某个工程中直接测试。
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;


type
TForm1 = class(TForm)
Button1: TButton;
RadioButton1: TRadioButton;
procedure Button1Click(Sender: TObject);
procedure Click1(sender:tobject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
op:array[0..10] of TRadioButton;
begin

for i:=0 to 10 do
begin
op:=TRadioButton.Create(self);
op.Parent:=self;
op.Top:=I*20+50;
op.Caption:=inttostr(i);
op.OnClick:=click1;//你的错误之处,添加这句话??????????????????
op.Visible :=true;
end;
end;

procedure TForm1.Click1(sender: tobject);
begin
if sender is TRadioButton then
showmessage(TRadioButton(sender).Caption );
end;

end.
你试一试这段代码就知道你的错误之处!
 
同意楼上!
还有你的Component建议用Control替换
如:
for i := 0 to ControlCount-1 do
begin
if (Controls is TRadioButton) and (Controls as TRadioButton).Checked then
showmessage(TRadioButton(Controls).Caption);
end;
 
seared2008的方法可行,谢谢iapollo的建议,希望以后能多与大家交流探讨。
 

为什么非要别人帮你做完?原理是一样的,自己的事应该自己做。
 
大家能帮我看看 关于 asp和sql server的帖子吗?
 

Similar threads

I
回复
0
查看
687
import
I
I
回复
0
查看
875
import
I
I
回复
0
查看
734
import
I
I
回复
0
查看
654
import
I
后退
顶部