Re: Pictures!

[ Related Articles and Replies ] [ Post Followup ] [ DelphiLand Discussion Forum ]

Posted by webmaster Guido on June 17, 2002 at 17:21:05:

In Reply to: Pictures! posted by Tara on June 01, 2002 at 10:54:49:

: I'm making a tutorial for a school Tech assignment, and I'm completely stuck.
: Basically, the tutorials are visual - there's an image, and a next button. Each time button is clicked, I want the image to change, until it has cycled through the entire tutorial. For example, I want tutorial001.bmp to change to tutorial002.bmp.
: I've played around for -ages-, and can't get anywhere..! The best I've got so far is this:
: procedure TTutorialBox.NextButtonClick(Sender: TObject);
: var tutorial: integer;
: count: integer;
: begin
: tutorial:= 2;
: count:=1;
: while count < 6 do
: begin;
: MapImage.Picture.LoadFromFile('tutorial00' + (IntToStr(tutorial)) + '.bmp');
: tutorial:= tutorial + 1;
: count:= count + 1;
: end;
: end;
: It changes the image once, but then stays at tutorial002.bmp. Should I be using arrays? I know this is a big ask, but hopefully, someone can help me.. thanks loads!

I assume that your first image "Tutorial001.bmp" is shown automatically, because it's loaded in the image-component at design time (and thus loaded once at runtime, right after the program starts). Now, the rest doesn't work, because the variables Tutorial and Count are "local" in the procedure NextButtonClick. Each time you click the button:

1. the variables Tutorial and Count are created;
2. Tutorial and Count are initialized;
3. "while Count < 6 do" means that this block of code is repeated until Count equals 6: Tutorial002.bmp is shown, a fraction of a second later Tutorial003.bmp is shown, and so on, until Tutorial006.bmp is shown (of course, you only see the last image);
4. the variables Tutorial and Count are destroyed.

The next time you click the button, this same sequence starts over again: show image 002, 003, 004, 005 and 006 in very rapid succession.

Here are some ideas for an improved structure of the program:

1. To start with, you only need 1 variable for the counter, not 2. Using two variables just makes it confusing and difficult to troubleshoot. For example, use just one integer variable named "Counter".
2. The value of Counter must be remembered between button-clicks. That's only possible if Counter is a "global" variable, meaning that you can access it from everywhere in the unit. A "global" variable must be declared outside of any routine, not *in* a procedure or a function like you did it.
3. When the program starts, Counter must be initialized, instead of doing this everytime that the button is clicked. The easiest way is to initialize Counter in the OnCreate event of the form.
4. When the button is clicked and Counter is not already at its maximal value, Counter should be incremented and a new image should be shown.
5. You could include a warning message in the button-click procedure that says that the user arrived at the last image. Tip: it's also nice to provide an extra button that resets the program to the first image.

Related Articles and Replies:

[ Related Articles and Replies ] [ Post Followup ] [ DelphiLand Discussion Forum ]