How to locate an object by name only?
I have 6 PictureBox
, so when saving the photo in the database, saved together in which PictureBox
it was.
Then, when loading the photos in a preview, I need each photo to be presented in its respective PictureBox
, I perform the query in the database assigning the values to the List
called VetorImagem
.
Return a array
from byte
and the name of the PictureBox
where the photo was.
So to mount the photo and display 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 locate PictureBox
?
For, when using vetorImagem[a].PictureBox_Name
I only have the name of the object.
1 answers
There are other ways to do this, but in your situation you can use Find ():
((PictureBox)this.Controls.Find("PictureBoxName", false)[0]).Image = Image.FromStream(stream);
Whereas PictureBox is directly in the Form.
If it is within a panel
, for example, Exchange the this
for the instance of the panel