none
Better method than using a label to store values

    Question

  • Hello. I am not very used to programming at all but i need to finish the project I'm working on. I had a list of labels from 1 to 100, and a list of images that show up in a picturebox. Whenever you saw one, its label counterpart's text would change to "seen". That way, the next time you saw that image, another textbox would read the "seen" text from the label and would inform that the image has been seen once already.

    Obviously that is not a very effective method but it worked out fine. The problem now is that i have to add 200 more pictures, which require me creating 200 more labels and adding a code to change each one of them, and that doesn't sound efficient at all.

    I'd like to know of a better place to do this, without using labels. For example, creating an invisible variable that stores a value like a label would but doesn't require physically creating it.

    Sunday, April 2, 2017 12:48 AM

Answers

  • Conceptually speaking a Dictionary would work. In the following code sample I don't make a label for each image nor are images selected at random so the focus is on the Dictionary.

    Can be downloaded here.

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace ImageAndSeen
    {
        public partial class Form1 : Form
        {
            Dictionary<string, bool> Items = new Dictionary<string, bool>();
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                openFileDialog1.InitialDirectory = System.IO.Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory, "Cars");
    
                label1.Text = "";
            }
            private void button1_Click(object sender, EventArgs e)
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    if (!Items.ContainsKey(openFileDialog1.FileName))
                    {
                        Items.Add(openFileDialog1.FileName, false);
                        label1.Text = "Fresh";
                    }
                    else
                    {
                        Items[openFileDialog1.FileName] = true;
                        label1.Text = "Seen";
                    }
                    pictureBox1.Load(openFileDialog1.FileName);
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by WeekSky Sunday, April 2, 2017 11:17 PM
    Sunday, April 2, 2017 1:39 AM
    Moderator

All replies

  • Hello,

    How are you storing images? And do you really need a label for each image?

    Seem you would have one PictureBox and one label to handle this. Would you be open to that?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, April 2, 2017 1:11 AM
    Moderator
  • I have 100 images stored on a folder. Whenever I press a button, one gets chosen randomly and is put in a picturebox. When that happens, if for example the image chosen was the number 45, the label15's text would change to "seen".

    That way, if the number 45 is chosen again, thanks to the label45 being "seen", a text next to the image would show up informing it's a repeated image.

    So yes, I need one label per picture.

    Sunday, April 2, 2017 1:21 AM
  • Conceptually speaking a Dictionary would work. In the following code sample I don't make a label for each image nor are images selected at random so the focus is on the Dictionary.

    Can be downloaded here.

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace ImageAndSeen
    {
        public partial class Form1 : Form
        {
            Dictionary<string, bool> Items = new Dictionary<string, bool>();
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                openFileDialog1.InitialDirectory = System.IO.Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory, "Cars");
    
                label1.Text = "";
            }
            private void button1_Click(object sender, EventArgs e)
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    if (!Items.ContainsKey(openFileDialog1.FileName))
                    {
                        Items.Add(openFileDialog1.FileName, false);
                        label1.Text = "Fresh";
                    }
                    else
                    {
                        Items[openFileDialog1.FileName] = true;
                        label1.Text = "Seen";
                    }
                    pictureBox1.Load(openFileDialog1.FileName);
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by WeekSky Sunday, April 2, 2017 11:17 PM
    Sunday, April 2, 2017 1:39 AM
    Moderator