I wrote this code for a simple app that gives us random images. The code works, but I feel like I'm doing something wrong. And one thing I would love to know is instead of grabbing from a folder called "resources", we would actually grab from a custom folder inside my exe. So there's no need to add any folder to a different computer, all works inside the exe.
- Is that possible to do ?
I'm slowly learning C#. Thank you for your time in reading my post.
P.S I'm very new in using the Lambda expression.
public partial class Menu : Form
{
private string[] files;
private string[] files2;
private string[] files3;
private string[] files4;
private int currentIndex = 0;
private int currentIndex2 = 0;
private int currentIndex3 = 0;
private int currentIndex4 = 0;
Random rnd = new Random();
public Menu()
{
InitializeComponent();
}
private void initializeImages()
{
string appRoot = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
files = System.IO.Directory.GetFiles(appRoot + @"\Resources");
files2 = System.IO.Directory.GetFiles(appRoot + @"\Resources");
files3 = System.IO.Directory.GetFiles(appRoot + @"\Resources");
files4 = System.IO.Directory.GetFiles(appRoot + @"\Resources");
files = files.OrderBy(x => rnd.Next()).ToArray();
files2 = files.OrderBy(x => rnd.Next()).ToArray();
files3 = files.OrderBy(x => rnd.Next()).ToArray();
files4 = files.OrderBy(x => rnd.Next()).ToArray();
}
private void setImage()
{
pictureBox1.ImageLocation = files[currentIndex];
pictureBox2.ImageLocation = files2[currentIndex2];
pictureBox3.ImageLocation = files3[currentIndex3];
pictureBox4.ImageLocation = files4[currentIndex4];
}
private void nextImage()
{
currentIndex = currentIndex < files.Length - 1 ? currentIndex + 1 : files.Length - 1;
setImage();
}
private void button1_Click(object sender, EventArgs e)
{
initializeImages();
nextImage();
}
}