How to find an object by name only?

1

I have 6 PictureBox , so when saving the photo in the database, saved together in which PictureBox it was.

Then, when loading photos into a view, I need each photo to be displayed in its PictureBox , I query the database by assigning the values to List called VetorImagem .

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

So to mount the photo and display it in 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 find PictureBox ? Well, using vetorImagem[a].PictureBox_Name I just got the object name.

    
asked by anonymous 19.05.2017 / 20:46

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);  

Considering that the PictureBox is directly in the Form.

If it is inside a panel , for example, Change this to panel

    
19.05.2017 / 21:01