Reputation: 1
I have a winform application (VS 2008) that has 4 picture boxes, one button (Upload). I want to picture boxes recorded images from a database (sql server 2005) in order from first to last, when I click the button. For example: press the button, the first picture box shows the first image, push the button, the second picture shows another picture box, etc.. and then that first picture box shows the fifth image.
pb1 = img1, pb2 = img2, pb3 = img3, pb4 = img4, pb1 = img5,..pb4 = img8,pb1 = img9,..etc..
So, there was a loop. I have this piece of code, but he recorded only one image in a picture box.
private void btnSHow_Click(object sender, EventArgs e)
{
SqlConnection connect = new SqlConnection("Data Source=JOHNO-PC\\SQLEXPRESS;Initial Catalog=DB_TraficSigns;Integrated Security=True");
SqlCommand command = new SqlCommand("SELECT picture FROM Tab_Sign ORDER BY id", connect);
SqlDataAdapter dp = new SqlDataAdapter(command);
DataSet ds = new DataSet("Tab_Sign");
byte[] MyData = new byte[0];
dp.Fill(ds, "Tab_Sign");
DataRow myRow;
myRow = ds.Tables["Tab_Sign"].Rows[0];
MyData = (byte[])myRow["picture"];
MemoryStream stream = new MemoryStream(MyData);
pb1.Image = Image.FromStream(stream);
}
Upvotes: 0
Views: 1479
Reputation: 12667
Instead of having pb1, pb2, pb3, etc., put your picture boxes in an array called something like pictureBoxes. Put the size of the array in a global, and use that size when declaring the array (so it's easily changable if you ever add or remove pictureboxes.)
int pictureBoxCount = 4;
int currentPictureBox = 0;
PictureBox[] pictureBoxes = new PictureBox[pictureBoxCount];
Then, every time you download a picture, update the array indexer.
currentPictureBox = (currentPictureBox + 1) % pictureBoxCount;
Then you can just do something like:
pictureBoxes[currentPictureBox].Image = Image.FromStream(stream);
Upvotes: 1