送分,简单问题,只有十行代码

chenzhou100 2002-07-23 11:20:02
procedure TForm1.Button1Click(Sender: TObject);
var
lun: string;
i: integer;
begin
lun := 'YDMNY';
for i := 1 to 5 do
begin
if copy(lun, i, 1) = 'Y' then
labeli.caption := '花生'
if copy(lun, i, 1) = 'N' then
labeli.caption := '无'
if copy(lun, i, 1) = 'M' then
labeli.caption := '小麦'
if copy(lun, i, 1) = 'D' then
labeli.caption := '水稻';
end;

end;

我知道是labeli这里出错,怎么改,附上代码者有分。
...全文
26 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lj_csdn 2002-07-23
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
lun : string;
k,I : Integer;
l : TLabel;
const
caps: array[1..4] of string=('花生','无','小麦','水稻');
begin
lun := 'YDMNY';
for I := 1 to 5 do //Length(lun) do
begin
k:=pos(lun[i],'YNMD');
if k>=0 then
begin
l:=TLabel(FindComponent('Label' + IntToStr(i)));
if l<>nil then l.Caption :=caps[k];
end;
end;
end;
flowersun 2002-07-23
  • 打赏
  • 举报
回复
var
lun: string;
i: integer;
mylabel:array[0..5] of tlabel;
begin
lun := 'YDMNY';
for i := 1 to 5 do
begin
mylabel[i]:=tlabel.Create(nil);
if copy(lun, i, 1) = 'Y' then
mylabel[i].caption := '花生'
else
if copy(lun, i, 1) = 'N' then
mylabel[i].caption := '无'
else
if copy(lun, i, 1) = 'M' then
mylabel[i].caption := '小麦'
else
if copy(lun, i, 1) = 'D' then
mylabel[i].caption := '水稻';
mylabel[i].Parent:=form1;
mylabel[i].Left:=20;
if i>1 then
mylabel[i].Top:=mylabel[i-1].Top+60;
mylabel[i].Visible:=true;
end;
end;
backlove 2002-07-23
  • 打赏
  • 举报
回复
写成这样,真行
AV_15 2002-07-23
  • 打赏
  • 举报
回复
还有,你大可不必像上面那样繁琐,每个 if 都用一对 begin end .
加个分号就行了.
AV_15 2002-07-23
  • 打赏
  • 举报
回复
你没用过 Delphi 吗,一行代码结束要 ; 啊!
这样:
在 '小麦','无',花生' 的后面都加上一个分号 ;
madyak 2002-07-23
  • 打赏
  • 举报
回复
在窗体上放上Tlabel,将NAME设为Labeli
procedure TForm1.Button1Click(Sender: TObject);
var
lun: string;
i: integer;
begin
lun := 'YDMNY';
for i := 1 to 5 do
begin
if copy(lun, i, 1) = 'Y' then
begin
labeli.caption := '花生'
if copy(lun, i, 1) = 'N' then
begin
labeli.caption := '无'
if copy(lun, i, 1) = 'M' then
begin
labeli.caption := '小麦'
if copy(lun, i, 1) = 'D' then
labeli.caption := '水稻';
end;
end;
end;
end;

end;

王集鹄 2002-07-23
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
lun: string;
I: Integer;
begin
lun := 'YDMNY';
for I := 1 to Length(lun) do
case UpCase(lun[I]) of
'Y':
if FindComponent(Format('Label%d', [I])) is TLabel then
(FindComponent(Format('Label%d', [I])) as TLabel).Caption := '花生';
'N':
if FindComponent(Format('Label%d', [I])) is TLabel then
(FindComponent(Format('Label%d', [I])) as TLabel).Caption := '无';
'M':
if FindComponent(Format('Label%d', [I])) is TLabel then
(FindComponent(Format('Label%d', [I])) as TLabel).Caption := '小麦';
'D':
if FindComponent(Format('Label%d', [I])) is TLabel then
(FindComponent(Format('Label%d', [I])) as TLabel).Caption := '水稻';
end;
end;

//你的信仰分怎么这样少?
liujidong 2002-07-23
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
lun: string;
i: integer;
temp: TLabel;
begin
lun := 'YDMNY';
for i := 1 to 5 do
begin
if copy(lun, i, 1) = 'Y' then
begin
temp:=FindComponent('label'+inttostr(i)) as tlabel;
temp.caption := '花生'
end
if copy(lun, i, 1) = 'N' then
begin
temp:=FindComponent('label'+inttostr(i)) as tlabel;
temp.caption := '无'
end
if copy(lun, i, 1) = 'M' then
begin
temp:=FindComponent('label'+inttostr(i)) as tlabel;
temp.caption := '小麦'
end
if copy(lun, i, 1) = 'D' then
begin
temp:=FindComponent('label'+inttostr(i)) as tlabel;
temp.caption := '水稻';
end
end;

end;

王集鹄 2002-07-23
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
lun: string;
I: Integer;
begin
lun := 'YDMNY';
for I := 1 to Length(lun) do
case UpCase(lun[I]) of
'Y':
if FindComponent(Format('Label%d', [I])) is TLabel then
(FindComponent(Format('Label%d', [I])) as TLabel).Caption := '花生';
'N':
if FindComponent(Format('Label%d', [I])) is TLabel then
(FindComponent(Format('Label%d', [I])) as TLabel).Caption := '无';
'M':
if FindComponent(Format('Label%d', [I])) is TLabel then
(FindComponent(Format('Label%d', [I])) as TLabel).Caption := '小麦';
'D':
if FindComponent(Format('Label%d', [I])) is TLabel then
(FindComponent(Format('Label%d', [I])) as TLabel).Caption := '水稻';
end;
end;

//你的信仰分怎么这样少?
qhdsfh 2002-07-23
  • 打赏
  • 举报
回复
你用的是LabeledEdit1对吗?你要命名label的caption,对吗?
如果是这样,应该用labeli.editlabel.caption := '文字';
lyqeast 2002-07-23
  • 打赏
  • 举报
回复
ft....你怎么把object当作自然语言来用啊....
和你这个最接近的就是label数组了
我很久没有用delphi了,你自己想想叭

5,391

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧