0

Got this problem. I have a form with 4 pictureboxes , when the form loads I want these 4 pictureboxes to be loaded with 4 different images form a textfile. I did it like this:

        optionOne.Image = new Bitmap(questionOne.getFoto(rand));

But if I take this same line of code for all m picture boxes and just change the optionOne to optionTwo (my second pictureBox) for example It just displays the same picture.

I'm getting my pictures from a textfile like this

      public string getFoto(int number)
    {
        stream = File.OpenText("Fotodd.txt");
        string[] fotos;
        string line = stream.ReadLine();
        fotos = line.Split('|');
        return fotos[number];
    }

And I've writen it like this in the textfile:

    1.jpg | 2.jpg | 3.jpg | ...

So how can I load different images in these different pictureBoxes with a textfile?

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
user3644837
  • 335
  • 2
  • 3
  • 8
  • This might be a silly question, but when you say that you're re-using that same line of code, are you also passing the same argument to `getFoto()` each time? – Joe Farrell May 18 '14 at 16:59
  • Yes and that's probably why all my pictureBoxes have the same image. How can I make it so that pictureBox1 shows 1.jpg , picturebox2 shows 2.jpg ... ? Or even better that they show randim images? – user3644837 May 18 '14 at 17:02
  • See [this answer](http://stackoverflow.com/questions/3975290/c-sharp-produce-a-random-number-in-a-range) for generating random numbers in a given range. – Joe Farrell May 18 '14 at 17:03

1 Answers1

1
int fotoCounter = 1;
public string getFoto(int number)
    {
        string line;
        using (var sr = new StreamReader("Fotodd.txt"))
        {
            line = sr.ReadLine();
        }
        string[] fotos;
        fotos = line.Split('|');
        return fotos[number *  fotoCounter - 1].Trim(); // String.Trim() removes whitespaces
    }

and call it like this:

string name = pictureBox1.Name;
optionOne.Image = new Bitmap(questionOne.getFoto(Convert.ToInt32(name[name.Length - 1])));

The name variable is to get the number of the pictureBox control; You'll have to make it more dynamic obviously since the control is not always pictureBox1. If you want a little help on this, please provide code that is before it. Once you're done updating all of the 4 PictureBox controls, increment the fotoCounter. I don't get why the array index - 1 doesn't work. The Array uses 0-based index which means if you want to get the first picture (1.jpg) it's actually fotos[0].

Method 2:

string name = (PictureBox)Control.Name;
string number = Convert.ToInt32(name[name.Length - 1]) * fotoCounter;
string file = number + ".jpg";
Asad Ali
  • 684
  • 6
  • 17
  • **number-1** doesn't work it gives me errors (outofbound). But when I use **number+1** it works. But now the problem still remains that my 4 pictureBoxes are showing the same image. But instead of showing **1.jpg** they are now showing **2.jpg** – user3644837 May 18 '14 at 16:58
  • Then remove `number + 1` as well. Where does rand come from? Is it different each time? – Asad Ali May 18 '14 at 16:59
  • When I remove it I get errors. And yes optionOne , optionTwo , ... those are my pictureBoxes – user3644837 May 18 '14 at 17:02
  • That makes no sense. You originally had `fotos[number]` and it worked fine, yes? Also, did you make sure `rand` is different each time that line of code is executed? – Asad Ali May 18 '14 at 17:03
  • yes it worked then. And no probably not , do you have an idea how I can achieve this? – user3644837 May 18 '14 at 17:07
  • You want to make it so `pictureBox1` has 1.jpg, `pictureBox2` has 2.jpg and so on, right? – Asad Ali May 18 '14 at 17:08
  • yes like that. and that when I click the button next on the form that --> pictureBox1 loads 5.JPG , pictureBox2 loads 6.jpg etc. Or it may be random whatever is easier – user3644837 May 18 '14 at 17:17
  • Sorry, i was eating. I'll update the code right away. – Asad Ali May 18 '14 at 17:36
  • @user3644837 I've updated the code, please have a look. – Asad Ali May 18 '14 at 17:46
  • Hey thanks for the help but when adding the code **return fotos ..** gives me a **system.IndexOutOfRangeException** – user3644837 May 18 '14 at 18:11
  • Remove the -1 at the end again. Also, if the last `char` of the control name is the name of the file + .jpg, why don't you directly retrieve the name of the control multiplied by `fotoCounter` and add `.jpg` at the end? I'll add the code above. – Asad Ali May 18 '14 at 18:13
  • I'm guessing this should work. the only thing that's not clear to me is the **Control.Name**. What do you mean by pictureBox control;? – user3644837 May 18 '14 at 18:21
  • You said that your picturebox controls are name as following: `pictureBox1`, `pictureBox2`, `pictureBox3`, `pictureBox4`. Now, since the files are just an integer (without the .jpg part), i used the last character from the name of PictureBoxes (1, 2, 3 or 4) and added .jpg to it. The string that comes out is the name of the file. If you're confused from `(PictureBox)Control.Name`, `Control` is one of the four existent PictureBoxes, I casted PictureBox to it and retrievted its name. – Asad Ali May 18 '14 at 18:23