How to locate an object by name only?

Asked

Viewed 365 times

1

I’m 6 PictureBox, then, when saving the photo in the database, save together in which PictureBox she was.

So when uploading the photos in a view, I need each photo to be displayed in its respective PictureBox, perform the query in the database assigning the values to the List called VetorImagem.

Return one array of byte and the name of PictureBox where the photo was.

Then to mount the photo and display on PictureBox, I use the code below:`

//Crio o List chamado VetorImagem.
List<Classes.Dictionary.Crm.List_Photo_Crm> vetorImagem = new List<Classes.Dictionary.Crm.List_Photo_Crm>();
//Instancio minha classe de consulto ao banco de dados.
Classes.Dictionary.Crm.Analise_Crm Dic_foto = new Classes.Dictionary.Crm.Analise_Crm();
//realizo a consulta e associo ao List VetorImagem
vetorImagem = Dic_foto.preenche_fotos(textEdit8.Text);`

for(int a = 0; a < vetorImagem.Count; a++)
{

            //defini um nome de arquivo
            string strNomeArquivo = Convert.ToString(DateTime.Now.ToFileTime());

            using (MemoryStream stream = new MemoryStream())
            {
                stream.Write(vetorImagem[a].photo, 0, vetorImagem[a].photo.Length);

                stream.Flush(); 

                vetorImagem[a].PictureBox_Name.Image = Image.FromStream(stream);
                vetorImagem[a].PictureBox_Name.SizeMode = PictureBoxSizeMode.StretchImage;

                stream.Close();
            }
}

However, how do I locate the PictureBox? Yeah, when using vetorImagem[a].PictureBox_Name I only have the name of the object.

  • you already have the 6 picturebox on the screen, or will put them at runtime ?

  • @Rovannlinhalis they already exist, I just need to "call them"

  • 1

    actually you don’t have to instantiate them, just locate the already existing instance in the form (is winforms correct?), see the answer I put up

  • Correct, perfect. Thank you

1 answer

2


There are other ways to do it, but in your situation you can use Find():

((PictureBox)this.Controls.Find("PictureBoxName", false)[0]).Image = Image.FromStream(stream);  

whereas Picturebox is directly in Form.

If you’re inside a panel, for example, change the this by the instance of panel

Browser other questions tagged

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