Traverse components of form C#

Asked

Viewed 2,047 times

0

Hello I have a table coming from the database, with several rows and columns. One of these columns is stored the address of the previously saved image. Now I need to go through all Picturebox and put in Imagelocation the address of that image. Oh, yes! Before I forget I have 10 Picturebox in the form. I’m using the following code:

foreach (Control item in this.Controls)  
{  
if (item is PictureBox)
{
item.Name.ToString();//Até aqui tudo certo, consigo ver nome dele, mas preciso pegar nome e a propriedade ImageLocation 
}

Someone has an idea?

  • It means that you have a list of image addresses in DB and you want to upload these images in the respective picture-boxes?

  • What variable the database information is loaded into. You are using some ORM: Entityframework, Nhibernate?

  • @Miguelangelo is a Datatable type variable, and I’m going through the rows and columns using a for

  • @Miguelangelo I’m using windows form C#.

  • But then for every Row traveled from Datatable, you want to upload the image in the next available Picturebox... would be this?

  • @Miguelangelo That’s right. + or - like this: Picturebox1.Imagelocation = Table.Rows[0][0]. Tostring(); Picturebox2.Imagelocation = Table.Rows[1][0]. Tostring();

  • @Fabríciosimonealanamendes See if any of the answers of that question help you.

  • @Qmechanic73 my code is already like this, I would really like to do this didactic.

Show 3 more comments

2 answers

0

You got this far without an answer. see how I solved my problem

//Primeiro percorri as colunas do datatable;
for (int i = 0; i < Tabela.Rows.Count; i++)
{
//Garanti que as células não estejam vazias
Celula = Tabela.Rows[i][0].ToString();
if (Celula != "")
{
//Usei um switch para analisar a variável i do for
switch (i)
{
case 0:
PictureBox1.ImageLocation = Tabela.Rows[i][0].ToString();
break;
case 1:
ptb2lugar.ImageLocation = Tabela.Rows[i]0].ToString();
break;
case2:
ptb3lugar.ImageLocation = Tabela.Rows[i][0].ToString();
break;
case 3:
PictureBox2.ImageLocation = Tabela.Rows[i][0].ToString();
break;
}
}
}

0


I’ll suggest you the following:

  1. create Picturebox dynamically, instead of adding them one by one by the designer:

    public partial class Form1 : Form
    {
        private PictureBox[] pictures;
    
        public Form1()
        {
            InitializeComponent();
    
            // criando as 10 PictureBox de que necessita
            this.pictures = Enumerable.Range(1, 10)
                .Select(this.CriarPictureBox)
                .ToArray();
    
            // eu estou adicionando dentro de um FlowLayout, mas poderia ser no Form,
            // ou dentro de qualquer outro contêiner de sua escolha
            this.flowLayoutPanel1.Controls.AddRange(this.pictures);
        }
    
        private PictureBox CriarPictureBox(int i)
        {
            return new PictureBox
            {
                Name = "pic" + i,
                Location = new Point(0, 0),
                Size = new Size(100, 100),
                BackColor = Color.Black,
                Visible = true,
            };
        }
    }
    
  2. with the Picturebox in an array, you can now refer to them using a numeric index, within the loading loop that reads the Datatable. Example:

    private void CarregarImagens(DataTable tabela, int primeiraRow)
    {
        for (int itPic = 0; itPic < this.pictures.Length; itPic++)
        {
            this.pictures[itPic].ImageLocation =
                tabela.Rows[itPic + primeiraRow]["NomeColuna"].ToString();
        }
    }
    

Browser other questions tagged

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