Hello,
Thanks for taking the time to help me, but before you do, I am relatively new at C# so please try to explain in detail your solution.
I'm trying to write a Windows Forms application that simulates rolling dice. The idea is to click a button that simulates casting dice and then shows the results in a label as well as displaying images in PictureBox Controls for each die.
I am stuck however on trying to load images from a directory relative to the program's current working directory, and would like to load them dynamically from that directory as needed rather than have them embedded in the application and toggling their visibility.
For some reason I can't post images even though my account is verified...
Here is the code for the form window:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace randomNumberGenerator { public partial class mainWindow : Form { /* * Would like to load images dynamically from the relative directory * * "\bin\die\{1:6, default}.png" * * with those file names, and test for their existance and permissions access */ // Load the application's current working directory (which directory is this application running from?) string cwd = System.IO.Directory.GetCurrentDirectory(); // Load the images using a combination of the CWD and the relative literal directories Image face1 = Image.FromFile(cwd + @"\bin\die\die1.png"); Image face2 = Image.FromFile(cwd + @"\bin\die\die2.png"); Image face3 = Image.FromFile(cwd + @"\bin\die\die3.png"); Image face4 = Image.FromFile(cwd + @"\bin\die\die4.png"); Image face5 = Image.FromFile(cwd + @"\bin\die\die5.png"); Image face6 = Image.FromFile(cwd + @"\bin\die\die6.png"); Image noface = Image.FromFile(cwd + @"\bin\die\default.png"); public mainWindow() { InitializeComponent(); } private void rollDice_Click(object sender, EventArgs e) { // Create a space for the value of Die 1 int die1; // Create a space for the value of Die 2 int die2; // Involke a random object for usage Random rand = new Random(); //Get a random integer and assign it to each die die1 = rand.Next(6); die2 = rand.Next(6); //Compensate for the possibility of zero by adding one to the dice die1++; die2++; //Display the textual results of each die diceResult.Text = "Die 1 has rolled a " + die1 + " and Die 2 has rolled a " + die2 + "."; diceResultTotal.Text = "Total: " + die1 + die2; /* * Wish to write a loop here that will involve using the switch dynamically on the values in die1 and die2 */ //Display the visual results of each die switch (die1) { case 1: dieOnePicture.Image = face1; break; case 2: dieOnePicture.Image = face2; break; case 3: dieOnePicture.Image = face3; break; case 4: dieOnePicture.Image = face4; break; case 5: dieOnePicture.Image = face5; break; case 6: dieOnePicture.Image = face6; break; default: dieOnePicture.Image = noface; break; } switch (die2) { case 1: dieTwoPicture.Image = face1; break; case 2: dieTwoPicture.Image = face2; break; case 3: dieTwoPicture.Image = face3; break; case 4: dieTwoPicture.Image = face4; break; case 5: dieTwoPicture.Image = face5; break; case 6: dieTwoPicture.Image = face6; break; default: dieTwoPicture.Image = noface; break; } } private void applicationQuit_Click(object sender, EventArgs e) { Application.Exit(); } } }