Slide Show Application in C#

In this article, I am demonstrating how to develop a slide show application using C#.

1. Open Visual Studio 2012.
2. Select "File" -> "New" -> "Project...".

New Project

3. Select the language as “Visual C#” and the template as “Windows Forms Application”, provide an appropriate name, I named it "SlideShowApp". Select the location and click on "OK".

Windows Forms Application

4. Place the Panel on the form and set the dock property to "Right".
5. Now place the PictureBox and set the dock property to "Fill".
6. Now design a form as in the following:

design a form

7. Drag and drop the FolderBrowserDiaglog and Timer control to the form. They will appear at the bottom. Go to the Timer control property and set Interval=3000.
8. Under the properties of next(>) ,previous(<) and Slide Show button, set the Enable property to False. So, by default, they should be disabled.
9. Prior to that you must import the namespace "System.IO".
10. Then write the following code:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.IO;

 

namespace SlideShowApp

{

    public partial class Form1 : Form

    {

        string Dirpath;

        int imgindex;

        public Form1()

        {

            InitializeComponent();

        } 

        private void Form1_Load(object sender, EventArgs e)

        {

            for (int i = 1; i < 10; i++)

            {

                comboBox1.Items.Add(i);

                comboBox1.SelectedIndex = 0;

            }

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            DialogResult dr = folderBrowserDialog1.ShowDialog();

            if (dr != DialogResult.Cancel)

            {

                listBox1.Items.Clear();

                Dirpath = folderBrowserDialog1.SelectedPath;

                string[] files = Directory.GetFiles(Dirpath, "*.Jpg");

                foreach (string file in files)

                {

                    int pos = file.LastIndexOf("||");

                    string FName = file.Substring(pos + 1);

                    listBox1.Items.Add(FName);

                }

                listBox1.SelectedIndex = imgindex = 0;

                btnprev.Enabled = true;

                btnnext.Enabled = btnshow.Enabled = true;

            }

        }

        private void btnshow_Click(object sender, EventArgs e)

        {

            if (btnshow.Text == "Slide Show")

            {

                btnshow.Text = "stop show";

                timer1.Interval = int.Parse(comboBox1.Text) * 1000;

                timer1.Start();

            }

            else

            {

                timer1.Stop();

                btnshow.Text = "Slide Show";

            }

        }

 

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            pictureBox1.ImageLocation = listBox1.SelectedItem.ToString ();

           // pictureBox1.ImageLocation = Dirpath + "\\" + listBox1.SelectedItem;

        }

 

        private void btnprev_Click(object sender, EventArgs e)

        {

            if (imgindex > 0)

            {

                imgindex -= 1;

                if(imgindex ==0)

                {

                    btnprev .Enabled=false;

                }

                if (imgindex < listBox1.Items.Count - 1)

                    btnnext.Enabled = true;

                listBox1.SelectedIndex = imgindex;

            }

        }

 

        private void btnnext_Click(object sender, EventArgs e)

        {

            if (imgindex < listBox1.Items.Count - 1)

            {

                imgindex += 1;

                if (imgindex == listBox1.Items.Count - 1)

                    btnnext.Enabled = false;

            if (imgindex > 0)

                btnprev.Enabled = true;                

                listBox1.SelectedIndex = imgindex;

            }

        } 

        private void timer1_Tick(object sender, EventArgs e)

        {

            btnnext.PerformClick();

        }

    }

}

11. Now save the application. Run the application. Click on the load images, browse to the folder and select the images folder. Click on "OK". Click on the (>)(<) button and check whether or not the previous and next are working. Click on the start show. The Show will then start and if you want to stop the show then  click on stop show.

12. I am showing a video demonstration too, to show you how it works.



I hope you liked my article.

If you want to see more articles then you may visit my blog:

dotnetbyabhipatil