The second step is to add a timer and create a slide show..
When the user selects a folder all of the picture files in the folder are displayed in the combo box.
When the user selects one of the pictures from the combo box, that picture is now displayed in the picture box.
Continue with the PictureViewer application.
Add the following control to the project:
You do not need to change any properties of the timer, we will enable the timer if there are any pictures to show.
- A timer: , timer1
Double click onTimer1 and write the code shown below:
private void timer1_Tick(object sender, EventArgs e)
{
// If there are items in the combo box change the index.
// Changing the index causes the selected picture to show.
if (cboPictures.Items.Count>0)
{
if(cboPictures.SelectedIndex < cboPictures.Items.Count-1)
{
// Go to next item.
cboPictures.SelectedIndex += 1;
}
else
{
// Go back to first item.
cboPictures.SelectedIndex =0;
}
}
}
Next, we need to modify the code for opening a folder so that the timer is enabled if any picture files were found.
Change this code
if (cboPictures.Items.Count > 0)
{
// Select the first item in the combo box.
cboPictures.SelectedIndex = 0;
cboPictures.Visible = true;
}
else {
cboPictures.Visible = false;
}
To this code:
// If no pictures were found in the selected folder don't display the combo box.
// If no pictures in combo box, don't enable the timer.
if (cboPictures.Items.Count > 0)
{
// Select the first item in the combo box.
cboPictures.SelectedIndex = 0;
cboPictures.Visible = true;
timer1.Interval = 6000;
timer1.Enabled = true;
}
else {
cboPictures.Visible = false;
timer1.Enabled = false;
}
Test your program and try opening a folder. You should see the names of the files, but the pictures don't display.
This project is now finished!
Save all before trying some of the enhancements below..
Complete code
To do: There are many little enhancements that you could add to this project:
- Let the user start and stop the slide show: add a menu item, click on the picture to stop the show.
- Let the user change the size of the picture box: Click + and - to make the size of the picture box change.
- Add an "about box" to the project.