Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello community,

I am using a variable called "progressCounter" in a game of hangman to run through a string[] to fetch words for the user to spell. progressCounter will start at 0, and increase++ until 4 (which will load the 5th and final word). I wish to apply a picture to a pictureBox representing the current score (in the form of apples, where each image will have 1 apple more than the last). progressCounter will provide an output at 5 to inform the user that they have passed all 5 spellings, and will then close the program to avoid crashing. progressCounter state 0 will not have an image.

My question is "what is the correct method of writing a filepath reference for the pictureBox to use the images on each of the following IF statements?" I have provided the function below, with an example of what I tried to construct:

C#
public void imageLoader()
       {
           if (progressCounter == 1)
           {
               pbxclue.Image = Image.FromFile(c:\\college/Y2/Visual_Programming/VS01/VP01/VP01/resources/pictures/1.jpg);
           }
           if (progressCounter == 2)
           {

           }
           if (progressCounter == 3)
           {

           }
           if (progressCounter == 4)
           {

           }
           if (progressCounter == 5)
           {

           }
       }


Any help is much appreciated, and as I am somewhat a novice at C#, please be kind.
Posted
Updated 17-Nov-15 7:37am
v2
Comments
Richard MacCutchan 17-Nov-15 13:49pm    
Why do you need the if statements? Just use a StringBuilder to create the path using the counter value as the filename.
Sergey Alexandrovich Kryukov 17-Nov-15 14:55pm    
And then the code using StringBuilder will use "if" internally, right? :-)
Nobody needs such paths and hard-coded 1, 2, 3 "options". Please see Solution 1.
—SA

Assuming this is a Windows Forms project:

1. use an ImageList Component

a. Drag an 'ImageList Component onto your Form from the 'Components hive of the ToolBox

b. at design-time add your images to the ImageList's 'Images collection; or, add them at run-time as needed.

2. you can then set the current image using the 'progressCounter directly ... since the progressCounter is #0 based:

pbxclue.Image = imageList1[progresscounter];
 
Share this answer
 
None. There are no situations when hard-coded path names can be useful. In all production-level applications, path names are calculated during runtime based on environment, content of some configuration file, user input, and the like. If you hard-code file paths, nothing can guarantee the applications to work on another machine, or simply next time.

If your images are constant, they belong to resources. Some recommendations: create *.resx resource and add images via "Add existing file". Better don't edit image files in Visual Studio, provide them as source files in your project. If you add the file from some outside directory, remember that it will be copied to the some project sub-directory, so make sure you are not creating duplicates. The file is added to the project and referenced in the project file and resource file as well. Then the auto-generated file will be created, it has all the declarations (static class with static properties), so everything is ready to use. Don't read from resource, just use the declarations in that auto-generated file.

Let me note that your code design is unacceptably bad (bad for maintenance). Again, the problem is hard-coded 1, 2, 3… and the sequence of "if". For working maintainable code, you would need to develop something much more regular, but it depends on what you want to achieve.

—SA
 
Share this answer
 
v2
Comments
Richard MacCutchan 18-Nov-15 3:45am    
Which is basically what I said.
Sergey Alexandrovich Kryukov 18-Nov-15 12:23pm    
I don't think so. It would be hard to read between lines. :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900