0

I am a complete newbie when it comes to programing. I followed the tutorial here to create a picture box. http://msdn.microsoft.com/en-u s/library/dd492135.aspx I work in c#. Now i am going through and making some changes. Is there anyway to make a code so that there can be a next and back button?

Any help would be appriciated.

Thanks, Rehan

  • 2
    yes there are.. you may want to show some example and ask another question – slow Feb 27 '18 at 08:29
  • What you'll want to do is switch the OpenFileDialog for a [FolderBrowserDialog](https://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.aspx), build a collection of images in the selected folder then the Next/Back buttons would step through the collection and update the display. Good luck! – Danielle Summers Feb 27 '18 at 08:34

1 Answers1

0

If I understand well you want to have two buttons (Next, Back) I make a little project and I will be happy if it helped you.

If you want this, continue reading:

enter image description here

First we have to declare imageCount and a List<string>: Imagefiles so we have this

List<string> Imagefiles = new List<string>();
int imageCount = 0;

imageCount helps to change images using buttons

Imagefiles contains all Image paths of our photos

Now to change photos we have to declare a path first which wil contains all photos. I use FolderBrowserDialog

using (var fbd = new FolderBrowserDialog())
{
    DialogResult result = fbd.ShowDialog();

    if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
    {
        findImagesInDirectory(fbd.SelectedPath);
    }
 }

You will see that I use findImagesInDirectory and this method doesnt exist. We have to create it. This method will help us to filter all the files from the path and get only the image files

private void findImagesInDirectory(string path)
{
    string[] files = Directory.GetFiles(path);
    foreach(string s in files)
    {
        if (s.EndsWith(".jpg") || s.EndsWith(".png")) //add more format files here
         {
            Imagefiles.Add(s);
         }
     }
     try
     {
         pictureBox1.ImageLocation = Imagefiles.First();
     }
     catch { MessageBox.Show("No files found!"); }

}

I use try because if no image files, with the above extension, exists code will break.

Now we declare all Image files (if they exists)

Next Button

    private void nextImageBtn_Click(object sender, EventArgs e)
    {
        if (imageCount + 1 == Imagefiles.Count)
        {
            MessageBox.Show("No Other Images!");
        }
        else
        {
            string nextImage = Imagefiles[imageCount + 1];
            pictureBox1.ImageLocation = nextImage;
            imageCount += 1;
        }
    }

Previous Button

    private void prevImageBtn_Click(object sender, EventArgs e)
    {
        if(imageCount == 0)
        {
            MessageBox.Show("No Other Images!");
        }
        else
        {
            string prevImage = Imagefiles[imageCount -1];
            pictureBox1.ImageLocation = prevImage;
            imageCount -= 1;
        }

    }

I believe my code help. Hope no bugs!

JexSrs
  • 155
  • 6
  • 18
  • The order in which files are retrieved is not always the order the user expects, though. I advise sorting the array at some point before using it. – Nyerguds Feb 27 '18 at 09:49
  • You have a point but, I cant think a possible code right now. If you have something in your mind, post it! – JexSrs Feb 27 '18 at 09:53
  • The `List` class simply has a `.Sort()` function which, without arguments, takes the default comparator. For `String`, that'd be normal alphabetical. Though it can also be given an `IComparer` object to use more advanced methods. I [know one here that sorts it the way Windows explorer does.](http://stackoverflow.com/a/3099659/395685) – Nyerguds Feb 27 '18 at 10:12