Adding value in the picturebox

Asked

Viewed 74 times

0

I wonder if there’s any way to make it work here

int chck = 0;
int fid = 0;
        while (chck < 150)
        {
            try
            {
                sqlcon.Open();
                string sql2 = "SELECT img FROM clienteimg WHERE ((clienteid = '" + cid + "') AND (id = '" + fid + "'))";
                SqlCommand cmd2 = new SqlCommand(sql2, sqlcon);
                SqlDataReader dr2 = cmd2.ExecuteReader();
                dr2.Read();
                byte[] imgLoc = (byte[])dr2[0];
                sqlcon.Close();
                MemoryStream mstream = new MemoryStream(imgLoc);
                pb1.Image = Image.FromStream(mstream);
            }
            catch (Exception)
            {
                sqlcon.Close();
                sqlcon.Open();
                string foid = "9";
                string sql3 = "SELECT img FROM ClienteImgDB WHERE id = '" + foid + "'";
                SqlCommand cmd3 = new SqlCommand(sql3, sqlcon);
                SqlDataReader dr3 = cmd3.ExecuteReader();
                dr3.Read();
                byte[] imgLoc = (byte[])dr3[0];
                sqlcon.Close();
                MemoryStream mstream = new MemoryStream(imgLoc);
                pb1.Image = Image.FromStream(mstream);
            }
            chck++;
            fid++;
        }

being that, there in Pb1.Image (picturebox1 for those who did not understand), I wanted the number to be flexible, I know that the following example does not work, but I hope you understand where I want to get

int chck = 0;
int fid = 0;
        while (chck < 150)
        {
            try
            {
                sqlcon.Open();
                string sql2 = "SELECT img FROM clienteimg WHERE ((clienteid = '" + cid + "') AND (id = '" + fid + "'))";
                SqlCommand cmd2 = new SqlCommand(sql2, sqlcon);
                SqlDataReader dr2 = cmd2.ExecuteReader();
                dr2.Read();
                byte[] imgLoc = (byte[])dr2[0];
                sqlcon.Close();
                MemoryStream mstream = new MemoryStream(imgLoc);
                pb'"+fid+"'.Image = Image.FromStream(mstream);
            }
            catch (Exception)
            {
                sqlcon.Close();
                sqlcon.Open();
                string foid = "9";
                string sql3 = "SELECT img FROM ClienteImgDB WHERE id = '" + foid + "'";
                SqlCommand cmd3 = new SqlCommand(sql3, sqlcon);
                SqlDataReader dr3 = cmd3.ExecuteReader();
                dr3.Read();
                byte[] imgLoc = (byte[])dr3[0];
                sqlcon.Close();
                MemoryStream mstream = new MemoryStream(imgLoc);
                pb'"+fid+"'.Image = Image.FromStream(mstream);
            }
            chck++;
            fid++;
        }

so that every time I rotate the loop, the "pb" is added in another 1 to fill the 150 pictureboxes I have

  • Seeing the code and even you explaining, I don’t understand what you mean by flexible number. What is the specific error you’re encountering? You want to create objects dynamically?

1 answer

1


You can use the method Find of the control or form in which the picturebox are inserted:

for (int i =0; i < 5; i++)
{
    ((PictureBox)this.Controls.Find("pb" + i, true)[0]).Image = null;
}

ps. Make sure there is control, otherwise you will receive an Exception.


Another option would be to generate the controls inside the loop, and then only generate the required amount (I would do so).

whereas the PictureBox will be inside the panel1:

panel1.Controls.Clear();
for (int i =0; i < 5; i++)
{
    PictureBox pb = new PictureBox();
    pb.Name = "pb" + i;
    pb.Image = null;
    pb.Parent = panel1;
    pb.Show();
}

Edit:

The first option, in your code would look like this:

try
{
    sqlcon.Open();
    string sql2 = "SELECT img FROM clienteimg WHERE ((clienteid = '" + cid + "') AND (id = '" + fid + "'))";
    SqlCommand cmd2 = new SqlCommand(sql2, sqlcon);
    SqlDataReader dr2 = cmd2.ExecuteReader();
    dr2.Read();
    byte[] imgLoc = (byte[])dr2[0];
    sqlcon.Close();
    MemoryStream mstream = new MemoryStream(imgLoc);
    ((PictureBox)this.Controls.Find("pb" + fid , true)[0]).Image = Image.FromStream(mstream);
}
...

Remembering that the name of your PictureBox shall be "pb"+ [each fid that you will carry]

The second option, yes, you will have to remove the Pictureboxes from the screen, because they will be useless. The recommendation is to use a Panel or Flowlayoutpanel to insert all Picturebox inside, so it is easy to organize, clean and insert.

But with the second option, it changes the logic you’re using, the Select would be different, among other things.

  • sorry but I never used this, I did not understand how to fit my "Try" in, I’m trying with your second option, something else, the pb I have so I should delete right? from what I understand the code there creates a pb every time, correct?

  • with the permission of the colleague, I edited the reply explaining how it would enter your code. I hope it helps.

  • worked out, only got a little slow pq has 150, but can be my pc, otherwise got top, vlw ai (:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.