Creating A Slide Show in VB and C#

This is a simple tutorial on how to create  slideshow in VB.NET and C#. We will use a continuous loop of images, as well as a next and previous button.

Part A – Design

Leave the default names for the Tool Box controls.

Sponsored Links

Start Visual Studio 2012/10 and choose your preferred programming language (C#/VB) to create a Windows form project.

Next resize Form1 so it is around 613 * 460, and in the layout section set the WindowState mode to Maximised. Then drag a ToolStrip from the toolbox and set its dock to bottom (fig 1). Next, add two ToolStrip buttons to the ToolStrip control, and align one left and one right.

In the properties pane of the ToolStrip buttons set the DisplayStyle to text for both buttons, and for the display text put < and > (our forward and back controls).

ToolStrip-Tasks

(fig 1)

Next add a picture box control and dock it in the parent container, click on the white/black triangle which appears and set the size mode to centerImage.

Next in the solutions explorer add a folder named images and put some images in it, and then highlight all the images. In the properties pane set the Copy to Output Directory to Copy if Newer. Your design should look like this:

ToolStrip Full

Finally we need to insert a timer, and in the properties pane set it to enabled and interval to 5000 (5 seconds).

Part B – Code

We need to declare two global variables. In the Public Class Form1 add the following code:

VB

Dim pictures() As String = {"apples.jpg", "flower1.jpg", "mountain.jpg", "sunset.jpg", "waterfall.jpg"}
Dim i As Integer = 0

C#

For C# this goes in  the public partial class Form1 : Form

string[] pictures = { "apples.jpg", "flower1.jpg", "mountain.jpg", "sunset.jpg", "waterfall.jpg" };
int i = 0;

The images are stored in an array, and the I will be used to increment/decrement the array index. Double click timer 1 and add the following code:

VB

i += 1

If pictures.Length = i Then
   i = 0
End If

PictureBox1.Image = Image.FromFile("images/" + pictures(i))

C#

i++;

if (pictures.Length == i)
   {
   i = 0;
   }

pictureBox1.Image = Image.FromFile("images/" + pictures[i]);

Here we increment I by 1 so the default value for I is 0, and it will increment every 5 seconds. Once  I equals the length of the array we set it back to 0 again, which avoids an IndexOutofRange Exception. Double click form1 and add this code:

VB

PictureBox1.Image = Image.FromFile("images/" + pictures(0))

C#

pictureBox1.Image = Image.FromFile("images/" + pictures[0]);

When the form loads we show the first images in the array. Remember that the timer will not show the first image because it is set to interval 5000 (5 seconds) and the value of I is incremented by 1 so I = 1, but arrays start at index 0. So, we display the first image when the form loads. For the toolstrip button, next add the following code:

VB

Timer1.Enabled = False

i += 1

If pictures.Length = i Then
   i = 0
End If

PictureBox1.Image = Image.FromFile("images/" + pictures(i))

Timer1.Enabled = True

C#

timer1.Enabled = false;

            i++;

if (pictures.Length == i)
{
   i = 0;
}

pictureBox1.Image = Image.FromFile("images/" + pictures[i]);

timer1.Enabled = true;

You should first disable the timer, as this prevents the timer from changing the next image. Increment I, check if I equals pictures.length; if it does set it to 0. Then load the image and enable the timer again, which will reset the timer and the next image will load in 5 seconds. Now we will do the same thing for the previous image button:

VB

Timer1.Enabled = False

If i = 0 Then
     i = pictures.Length
End If

i -= 1

PictureBox1.Image = Image.FromFile("images/" + pictures(i))

Timer1.Enabled = True

C#

timer1.Enabled = false;

if (i == 0)
{
    i = pictures.Length;
}
   i--;
         
pictureBox1.Image = Image.FromFile("images/" + pictures[i]);

timer1.Enabled = true;

This is similar to the above example, however this time we are going back. We also need to check if I = 0; if it does set I to equal pictures to length. If you don’t do this check, I will result in -1 which will cause IndexOutOfRange Exception.

Resources

No License restrictions, however images are from Wikimedia commons. Please see the images.txt file for license.

C# - SlideShow

VB - SlideShow