From: "Earl F. Glynn" Subject: Re: How do you refer to an image using a variable? Date: 30 May 2000 00:00:00 GMT Message-ID: <1dXY4.7215$Zm5.474457@bgtnsc06-news.ops.worldnet.att.net> References: <8h0ni9$dt2$1@nnrp1.deja.com> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6700 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc06-news.ops.worldnet.att.net 959725501 12.75.140.36 (Tue, 30 May 2000 22:25:01 GMT) Organization: AT&T Worldnet X-MSMail-Priority: Normal NNTP-Posting-Date: Tue, 30 May 2000 22:25:01 GMT Newsgroups: comp.lang.pascal.delphi.misc wrote in message news:8h0ni9$dt2$1@nnrp1.deja.com... > First, . . . thanks for any help! > > > Situation: > I have ten images whose names are imgQ1 . . . imgQ10. Why not just have an array of images and use an index? Put five TImages on a form and two TButtons. Set Button1.Tag to 2 and Button2.Tag to 4 (or whatever). Wire the OnClick events for these buttons to the ButtonClick method below. You can access any TImage with just a single number: unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Image1: TImage; Image2: TImage; Image3: TImage; Image4: TImage; Image5: TImage; Button1: TButton; Button2: TButton; procedure FormCreate(Sender: TObject); procedure ButtonClick(Sender: TObject); private ImageArray: ARRAY[1..5] OF TImage; public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); begin // "wire up" array elements. ImageArray is an array of pointers // to the TImage instances. ImageArray[1] := Image1; ImageArray[2] := Image2; ImageArray[3] := Image3; ImageArray[4] := Image4; ImageArray[5] := Image5; end; procedure TForm1.ButtonClick(Sender: TObject); VAR Bitmap: TBitmap; index : INTEGER; begin index := (Sender AS TButton).Tag; // Use in-memory bitmap for "double buffering" (avoid flicker) Bitmap := TBitmap.Create; TRY Bitmap.Width := ImageArray[index].Width; Bitmap.Height := ImageArray[index].Height; Bitmap.PixelFormat := pf24bit; Bitmap.Canvas.Brush.Color := clRed; Bitmap.Canvas.FillRect(Bitmap.Canvas.ClipRect); ImageArray[index].Picture.Graphic := Bitmap FINALLY Bitmap.Free END end; end. -- efg Earl F. Glynn Overland Park, KS USA efg's Computer Lab: http://www.efg2.com/Lab