delphi 动态生成控件名的问题
我窗体上有10个Image名字分别是Image1到Image10还有个文本框和一个按钮我想在文本框里输入数字点按钮时让某一个Image加载1.bmp其他的加载2.bmp比...
我窗体上有10个Image 名字分别是Image1到Image10 还有个文本框 和一个按钮 我想在文本框里输入数字 点按钮时 让某一个Image加载1.bmp 其他的加载2.bmp
比如我输入“2” 那Image2加载的就是1.bmp 其他的都是2.bmp
按钮事件怎么写 展开
比如我输入“2” 那Image2加载的就是1.bmp 其他的都是2.bmp
按钮事件怎么写 展开
1个回答
展开全部
注意两方法要求edit1要限制输入数字!
方法1.
procedure TForm1.Button1Click(Sender: TObject);
begin
Image1.Picture.LoadFromFile('2.bmp');
Image2.Picture.LoadFromFile('2.bmp');
Image3.Picture.LoadFromFile('2.bmp');
Image4.Picture.LoadFromFile('2.bmp');
Image5.Picture.LoadFromFile('2.bmp');
Image6.Picture.LoadFromFile('2.bmp');
Image7.Picture.LoadFromFile('2.bmp');
Image8.Picture.LoadFromFile('2.bmp');
Image9.Picture.LoadFromFile('2.bmp');
Image10.Picture.LoadFromFile('2.bmp');
case strtoint(trim(edit1.Text)) of
1: Image1.Picture.LoadFromFile('1.bmp');
2: Image2.Picture.LoadFromFile('1.bmp');
3: Image3.Picture.LoadFromFile('1.bmp');
4: Image4.Picture.LoadFromFile('1.bmp');
5: Image5.Picture.LoadFromFile('1.bmp');
6: Image6.Picture.LoadFromFile('1.bmp');
7: Image7.Picture.LoadFromFile('1.bmp');
8: Image8.Picture.LoadFromFile('1.bmp');
9: Image9.Picture.LoadFromFile('1.bmp');
10: Image10.Picture.LoadFromFile('1.bmp');
end;
end;
方法2.动态数组生成
implementation
var
flag: string;
imgname:array[1..10] of TImage;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
i,j: integer;
begin
j:= strtoint(trim(edit1.text));
if (j<0) or (j>10) then exit;
for i:=1 to 10 do
begin
if flag <> 'true' then
begin
imgname[i]:= TImage.Create(nil);
imgname[i].Parent:= Form1;
imgname[i].SetBounds(i*60,25,13,13);
end;
imgname[i].AutoSize:=true;
imgname[i].Picture.LoadFromFile('2.bmp');
end;
flag:='true';
imgname[j].Picture.LoadFromFile('1.bmp');
end;
方法1.
procedure TForm1.Button1Click(Sender: TObject);
begin
Image1.Picture.LoadFromFile('2.bmp');
Image2.Picture.LoadFromFile('2.bmp');
Image3.Picture.LoadFromFile('2.bmp');
Image4.Picture.LoadFromFile('2.bmp');
Image5.Picture.LoadFromFile('2.bmp');
Image6.Picture.LoadFromFile('2.bmp');
Image7.Picture.LoadFromFile('2.bmp');
Image8.Picture.LoadFromFile('2.bmp');
Image9.Picture.LoadFromFile('2.bmp');
Image10.Picture.LoadFromFile('2.bmp');
case strtoint(trim(edit1.Text)) of
1: Image1.Picture.LoadFromFile('1.bmp');
2: Image2.Picture.LoadFromFile('1.bmp');
3: Image3.Picture.LoadFromFile('1.bmp');
4: Image4.Picture.LoadFromFile('1.bmp');
5: Image5.Picture.LoadFromFile('1.bmp');
6: Image6.Picture.LoadFromFile('1.bmp');
7: Image7.Picture.LoadFromFile('1.bmp');
8: Image8.Picture.LoadFromFile('1.bmp');
9: Image9.Picture.LoadFromFile('1.bmp');
10: Image10.Picture.LoadFromFile('1.bmp');
end;
end;
方法2.动态数组生成
implementation
var
flag: string;
imgname:array[1..10] of TImage;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
i,j: integer;
begin
j:= strtoint(trim(edit1.text));
if (j<0) or (j>10) then exit;
for i:=1 to 10 do
begin
if flag <> 'true' then
begin
imgname[i]:= TImage.Create(nil);
imgname[i].Parent:= Form1;
imgname[i].SetBounds(i*60,25,13,13);
end;
imgname[i].AutoSize:=true;
imgname[i].Picture.LoadFromFile('2.bmp');
end;
flag:='true';
imgname[j].Picture.LoadFromFile('1.bmp');
end;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询