How to go through an array at each click on C#?

Asked

Viewed 135 times

3

I’m trying to make a Image Viewer for multi page Tiffs. I’m wearing a PictureBox and two buttons - one to move down and to the next page, and one to move up and to the previous page.

My code goes through the array in question but when I press the button down, it goes through the array and goes to the last page of the TIFF instead of going through one page at a time.

This is the code of my down button - downbut:

int tamanho;
int idx1 = 1;
public string[] filestiff; 
private void downbut_Click(object sender, EventArgs e)
    {
        tamanho = filestiff.Length;
        do {
            foreach (string file in filestiff) //para cada ficheiro no array
            {
                picturetiff.InitialImage = null;
                Image a = Image.FromFile(file);
                picturetiff.Image = a;
                idx1++;
            }
        } while (idx1 < tamanho);
        if (idx1 == tamanho) //quando chegar ao fim do array
        {
            idx1 = 1;
            downbut.Enabled = false;
            upbut.Enabled = true;
        }
    }

What I have to change in my code so that, at each click on the button, walk only one page instead of all at once?

Thanks to those who help!

1 answer

3


You don’t need a loop inside the button, you just have to assign the new image in the click. Example:

int idx1 = 0; //Primeira imagem, posição 0
string[] filestiff; 
private void downbut_Click(object sender, EventArgs e)
{
   if (idx < filestiff.Length-1) //só avança se o índice atual for menor que o último índice
   {
       idx++;
       Image a = Image.FromFile(filestiff[idx]);
       picturetiff.Image = a;
   }
}
private void upbut_Click(object sender, EventArgs e)
{
   if (idx > 0) //só volta se o índice atual for maior que 0
   {
       idx--;
       Image a = Image.FromFile(filestiff[idx]);
       picturetiff.Image = a;
   }
}
  • That’s exactly what it was! Thank you so much!

  • you can mark as reply @Sofiarodrigues, for nothing =]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.