Search code examples
c#filedirectoryfileinfoimagelist

How to get image file name in a textbox?


I have a problem about C# file name. I show some pictures with PictureBox. Also I want to write picture's name in a TextBox. I search fileinfo, directoryinfo, but it doesnt work.

List<Image> images = new List<Image>();
 images.Add(Properties.Resources.baseball_bat);
 images.Add(Properties.Resources.bracelet);
 images.Add(Properties.Resources.bride);

pictureBox1.Image = images[..];

and i want to write baseball_bat, bride, bracelet etc. in a TextBox. What can I do? any offer?


Solution

  • This functionality is already built in...

    Image lists store their images in a way that you can reference them by name and return the image.

    Single use :

    private string GetImageName(ImageList imglist, int index)
        {
            return imglist.Images.Keys[index].ToString();
        }
    

    This returns the name of the image at the passed index

    Store values for later on :

    private Dictionary<int, string> GetImageNames(ImageList imglist)
        {
            var dict = new Dictionary<int, string>();
            int salt = 0;
    
            foreach (var item in imglist.Images.Keys)
            {
                dict.Add(salt, item.ToString());
                salt++;
            }
            return dict;
        }
    

    This returns a dictionary that references the index of picture to their string name in the image list.

    Once again this is built in there is no need to try to extend funct. to it...