Today is the first day of the course Programming 2 (C#) and I need to display a small jpg 25 times in a 5×5 grid using 2 embedded for loops. The first course only covered console apps so this is my first foray into Forms and using external files and this is the first exercise in the book.
I have the program working but the images display in a single row instead of 5 rows of 5. Not entirely sure what is going wrong as the for loops should be correct as far as i can see(?) I know it’s going to be something really stupid and obvious!
Here’s my code…
namespace Ovning1._3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int x = 0, y = 0, width = 50, height = 50;
PictureBox[,] pictures = new PictureBox[5,5];
for (int r = 0; r < 5; r++)
{
for (int c = 0; c < 5; c++)
{
pictures[r,c] = new PictureBox();
pictures[r,c].Left = x;
pictures[r,c].Top = y;
pictures[r,c].Width = width;
pictures[r,c].Height = height;
pictures[r,c].BackgroundImage = Image.FromFile("C:\Users\iamdu\Documents\School\Programmering 2\Ovning1.3\Ovning1.3\resources\images\flower.jpg");
pictures[r,c].Click += PbxFlower_Click;
this.Controls.Add(pictures[r,c]);
x += width;
}
}
}
private void PbxFlower_Click(object sender, EventArgs e)
{
PictureBox clickedFlower = (PictureBox)sender;
clickedFlower.BorderStyle = BorderStyle.FixedSingle;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
I’ve stared at the code for the last hour and have no idea what I’m doing wrong. The book has not given any information about how to use Forms and only briefly talks about 2d arrays, which are also new to me and were not covered in the first course.
i am Dubers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.