Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

C# ListView + ImageList Question

Tdata

Sponsor

I am creating a Form that uses a ListView Control in the Large Icon Setting, Build_List. I am using an ImageList for the Icons, imageList1. My question is, can you use relative paths with an ImageList? <- Not sure if I'm even using the word right...

An Example:
Code:
string bhouse = "\\images\\bhouse.gif";

That type of relative...

Since the App is still in development, I am using Generic Icons for the List. I would like to be able to use the ImageList in such a way that it doesn't compile the Icons into the program. I need to be able to change the Icons on the fly without having to recompile. It needs to pull them from the folder directly.

Any Ideas?
 
Are you asking how you obtain the images for the items from the folder similarly as Windows Explorer does?

Edit: Perhaps a better question is: if you're reading a folder for an image source, what are the images used for?

Secondly: Relative paths are associated to the active directory of the running application by means of ".\" not "\". "\" by itself means 'Root directory', or, if the drive was "C:", then "\" by itself means "C:\" on windows systems. Further, "\\images\\bhouse.gif" would mean "C:\\images\\bhouse.gif" if the active drive was "C:". I believe similar concepts apply to Linux; however, that's not my area.

PS: As an off note, you can use '@' before strings to remove the need to double backslash:
@"\images\bhouse.gif"
 

Tdata

Sponsor

I'd like to have the application get the image files I use for the icons from the image directory at runtime, instead of them being complied into the program. This way I can just place each new icon image into the image folder, replacing the current one and have it show up correctly.
 
Can I see the code you're currently using, or are you asking how it's done, period?
Because, from my understanding of the ImageLists, they don't directly load images from the hard drive, you add them individually, and you'd basically scan the contents of the folder and add each item to the ImageList individually.

You can use System.IO.DirectoryInfo to obtain a list of the files for the current directory:
Code:
[color=#800080]DirectoryInfo[/color] [color=#008080]currentDir[/color] [color=#808000]=[/color] [color=#0000FF]new[/color] [color=#800080]DirectoryInfo[/color][color=#808080]([/color][color=#008000]".\\images\\"[/color][color=#808080])[/color][color=#808080];[/color]

[color=#0000FF]if[/color] [color=#808080]([/color][color=#008080]currentDir[/color][color=#808080].[/color][color=#008080]Exists[/color][color=#808080])[/color]

[color=#808080]{[/color]

    [color=#800080]FileInfo[/color][color=#808080][[/color][color=#808080]][/color] [color=#008080]imageFiles[/color] [color=#808000]=[/color]

        [color=#008080]currentDir[/color][color=#808080].[/color][color=#008080]GetFiles[/color][color=#808080]([/color][color=#008000]"*.jpg"[/color][color=#808080])[/color][color=#808080].[/color][color=#008080]Concat[/color][color=#808080]([/color]

        [color=#008080]currentDir[/color][color=#808080].[/color][color=#008080]GetFiles[/color][color=#808080]([/color][color=#008000]"*.png"[/color][color=#808080])[/color][color=#808080])[/color][color=#808080].[/color][color=#008080]Concat[/color][color=#808080]([/color]

        [color=#008080]currentDir[/color][color=#808080].[/color][color=#008080]GetFiles[/color][color=#808080]([/color][color=#008000]"*.gif"[/color][color=#808080])[/color][color=#808080])[/color][color=#808080].[/color][color=#008080]Concat[/color][color=#808080]([/color]

        [color=#008080]currentDir[/color][color=#808080].[/color][color=#008080]GetFiles[/color][color=#808080]([/color][color=#008000]"*.bmp"[/color][color=#808080])[/color][color=#808080])[/color][color=#808080].[/color][color=#008080]OrderBy[/color][color=#808080]([/color][color=#008080]image[/color] [color=#808000]=[/color][color=#808000]>[/color] [color=#008080]image[/color][color=#808080].[/color][color=#008080]Name[/color][color=#808080])[/color][color=#808080].[/color][color=#008080]ToArray[/color][color=#808080]([/color][color=#808080])[/color][color=#808080];[/color]

    [color=#0000FF]foreach[/color] [color=#808080]([/color][color=#800080]FileInfo[/color] [color=#008080]file[/color] [color=#0000FF]in[/color] [color=#008080]imageFiles[/color][color=#808080])[/color]

    [color=#808080]{[/color]

        [color=#0000FF]string[/color] [color=#008080]currentFileName[/color] [color=#808000]=[/color] [color=#008080]file[/color][color=#808080].[/color][color=#008080]FullName[/color][color=#808080];[/color]

        [color=#008080]Image[/color] [color=#008080]currentImage[/color] [color=#808000]=[/color] [color=#008080]Image[/color][color=#808080].[/color][color=#008080]FromFile[/color][color=#808080]([/color][color=#008080]currentFileName[/color][color=#808080])[/color][color=#808080];[/color]

        [color=#008000]/* *

         * Add code here to add them to the image-list.

         * *

         * Example:

         * imageList1.Images.Add(Path.GetFileName(currentFileName), currentImage);

         * */[/color]

    [color=#808080]}[/color]

[color=#808080]}[/color]

Note, if you're using a version of C♯ earlier than 3.0, then you have to merge and sort the arrays yourself.
If you're using C♯ 3.0, include the namespace 'System.Linq' to allow the Concat, OrderBy and ToArray methods to work.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top