1

I have tried using Tag, Name, Text, IndexOf. Every time, I get the first name and that's it. Here is the code:

// Perform scanning
for (;;)
{
    List<System.Drawing.Image> images = this.ScannerDevice.PerformScan().ToList();

    // Show picture in window
    this.Invoke((MethodInvoker)delegate
    {
         this.FrontImage = images[0];

         foreach (System.Drawing.Image image in images)
         {
               PictureBox pf = new PictureBox();
               pf.SizeMode = PictureBoxSizeMode.StretchImage; pf.Height = 150; pf.Width = 170;                                                          
               pf.Image = image;                        
               pf.Click += new EventHandler(pictureClicked);
               flowLayoutPanel1.Controls.Add(pf);
               pf.Tag=flowLayoutPanel1.Controls.Count;
         }                            
         ScanFinishedEventArgs eventArgs = new ScanFinishedEventArgs { AcceptScan = true };
         this.ScanFinished?.Invoke(this, eventArgs);
         label1.Text = Convert.ToString(flowLayoutPanel1.Controls.Count);
    });                       
}

void pictureClicked(object sender, EventArgs e)
{
      if (selectedPicture != null)
          selectedPicture.BorderStyle = BorderStyle.None;
      selectedPicture = (PictureBox)sender;
      selectedPicture.BorderStyle = BorderStyle.FixedSingle;            
      pictureBox1.Image = selectedPicture.Image;
      label2.Text = Convert.ToString(pf.Tag);
}

Also I would like to use that name later to be displayed in another label when I click on the certain picturebox. Also I have tried using anonymous types but unable to use it with image objects. What am I doing wrong?

Harun Ćerim
  • 893
  • 1
  • 12
  • 23
  • this might help you to understand and write the code about [get the index of the current iteration of a foreach loop](http://stackoverflow.com/questions/43021/how-do-you-get-the-index-of-the-current-iteration-of-a-foreach-loop) – Mohit Shrivastava Feb 07 '17 at 09:58
  • 1
    What exactly do you want to display? The name of the image file? You need to store it right when you read it. Maybe in the Image.Tag. Also: foreach is nice, but when you need the index, forget it and use a for loop instead!! Also: Use a Labda with a common click handler.. – TaW Feb 07 '17 at 09:59
  • He suggests using for loop because concept of an index is foreign to enumeration. I want to display all the images from the list in little pictureboxes in flowlayoutpanel. I already have the click event but what I need to do is to have the number of all images from the list displayed in one label and the name(number) of the currently clicked picture in another label. Also I need to use foreach loop. – Harun Ćerim Feb 07 '17 at 10:03
  • 1
    _Also I need to use foreach loop._ Why?? You can add the current FLP.Controls count as the PB.Tag: `pf.Tag=flowLayoutPanel1.Controls.Count;` – TaW Feb 07 '17 at 10:06
  • @TaW It worked. Now, my question is how to assign that Tag for each picturebox so when I click on it the current `pf.Tag` is displayed in another label. I updated my question and added click event. – Harun Ćerim Feb 07 '17 at 13:47
  • 1
    change pf.Tag to selectedPicture.Tag Also: Do a if (selectedPicture != null) selectedPicture.BorderStyle = BorderStyle.None at the start of the event to 'disselect' the previously clicked pbox.. – TaW Feb 07 '17 at 13:53
  • Yeah. It worked. Thank you very much. I just wonder why that basic indexing didn't work. I've read Jon Skeet's answer which says that plenty of collections don't support direct indexing and that `foreach` doesn't always use an iterator at all. But this alternative way really solved my problem. Thank you again. – Harun Ćerim Feb 07 '17 at 14:02

1 Answers1

1

I've just done this and it seems to work. 3 images are added, each named according to the index value at the time. A label is set showing the image count. The picture clicked handler displays the name in a message box when the image is clicked.

private void button1_Click(object sender, EventArgs e)
{
    int index = 0;

    foreach (Image image in images.Images)
    {
        PictureBox pf = new PictureBox();
        pf.SizeMode = PictureBoxSizeMode.StretchImage;
        pf.Height = 50;
        pf.Width = 50;
        pf.Click += new EventHandler(PictureClicked);
        pf.Name = index.ToString();
        pf.Image = image;
        flowLayoutPanel1.Controls.Add(pf);

        index++;
    }

    lblImagecount.Text = index.ToString();
}

private void PictureClicked(object sender, EventArgs e)
{
    MessageBox.Show(((PictureBox) sender).Name);
}
Wheels73
  • 2,650
  • 1
  • 7
  • 16
  • How many images do you have in your image list? I manually added 3 in! Yours also looks different. If images is your image list, then shouldn't you be accessing the images collection in the loop like my example? Unless images is the collection and its passed in. – Wheels73 Feb 07 '17 at 12:51
  • I don't know the number of images. I acquire them through scanning process. I add bunch of documents in feeder and that's it. – Harun Ćerim Feb 07 '17 at 12:54
  • Suggest you put a break point on the for loop and check how many images are in the list. – Wheels73 Feb 07 '17 at 12:55
  • Ok.. but I think the question is still.. is the scanner creating you a list of more than one image? The for loop you've done is sound! – Wheels73 Feb 07 '17 at 13:41