还是老问题,为何累计groupbox内的各edit值不能循环累计后送到groupbox的caption? 100分送了!(100分)

  • 主题发起人 sagitary
  • 开始时间
S

sagitary

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手,小弟几天前问了一个groupbox内的edit值如何被送到groupbox的caption上的问题,
但现在发现不能循环,即每次在edit内按回车后,程序自动累计该edit所属的groupbox内
所有edit的数值,并将其送到该groupbox的caption上,程序编译通过,但调试时发现
程序刚到循环就自动跳出,如果把循环变量i改为数值则会报"list index out of bound”错,
查Tcomponent的help,发现Tcomponent属于Dsgnintf这个unit,在程序前面加上Dsgnintf后
又报错没找到该unit,是不是我的delphi5版本有错还是什么,请高手们回答,希望
周一能看到热心同志。多谢!!!
界面上一个groupbox内有三个edit,但程序运行后,groupbox的caption总是0;
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
I,total:integer;
begin
if key=vk_return then
begin
total:=0;
with (sender as Tedit).parent as Tgroupbox do
begin
for i:=0 to componentcount-1 do
begin
if components is Tedit then
total:=total+strtoint((components as Tedit).text);
end;
caption:=inttostr(total);
end;
end;
end;
 
在设计期的控件的父全是form
所以要用Controls而不是components
把components改为Controls就OK了
 
多谢! 改成下面的代码后成功了,但不知以前的许多贴子上写的都是components呢?
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
I,total:integer;
begin
if key=vk_return then
begin
total:=0;
with (sender as Tedit).parent as Tgroupbox do
begin
for i:=0 to controlcount -1 do
begin
if controls is Tedit then
total:=total+strtoint((controls as Tedit).text);
end;
caption:=inttostr(total);
end;
end;
end;
 
顶部