Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a Panel in Visual C#. At Runtime on page_Load event i added PictureBox to Panel dynamically. Now Picture added successfully to that Panel as follow
C#
private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt =controller.Category_SelectList();
            List<picturebox> pictureBoxList = new List<picturebox>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                PictureBox picture = new PictureBox
                {
                    Name = "pictureBox" + i,
                    
                    Size = new Size(100, 100),
                    Location = new Point(i * 105, 1),
                    BorderStyle = BorderStyle.FixedSingle,
                    SizeMode = PictureBoxSizeMode.Zoom
                   
                };
                image = (byte[])dr["c_CatPhoto"];
                picture.Image = ByteArrToImage(image);
                picture.Name=dr["c_CatName"].ToString();
                Label lb = new Label();
                lb.Name = "label";
                lb.Text = dr["c_CatName"].ToString();
                lb.Size = new System.Drawing.Size(100, 40);
                lb.Location = new Point(i * 105, 105);
                
                //picture.ImageLocation = dr["c_CatPhoto"].ToString();
                pictureBoxList.Add(picture);
                pnlDisplayImage.Controls.Add(lb);

            }

            foreach (PictureBox p in pictureBoxList)
            {
                pnlDisplayImage.Controls.Add(p);
            }
        }
      
        public Image ByteArrToImage(byte[] brr)
        {
            MemoryStream ms = new MemoryStream(brr);
            Image img = Image.FromStream(ms);
            return img;
        }

I want to show related data into datagridview retrieve form database when i clicked dynamic picturebox.
But I have problem how to Generate event (Such as PictureBox_Click ) on Click that Dynamic PictureBox and how can i get image value. Please Give me Code and Instruction, how to write event code for PictureBox in my Visual C# Desktop Application.
Posted
Updated 16-Nov-14 17:17pm
v2
Comments
DamithSL 16-Nov-14 23:20pm    
what is the primary key or unique key for the data?

1 solution

C#
//create click event as below 
picture.Click += this.PictureClick;
//after add the picture to pictrebox 
pictureBoxList.Add(picture);

you can create event as below
C#
private void PictureClick(object sender, EventArgs e) {
    PictureBox oPictureBox = (PictureBox)sender;
    // if name is unique value for the data, you can get the name 
    // and find that name in your datatable
    string c_CatName = oPictureBox.Name;

}


if you have some other unique key column, try using Tag[^] property of PictureBox and set it as the unique key value, then you can get it inside event handler and take the details from datatable based on that unique key.
 
Share this answer
 
v2
Comments
BillWoodruff 17-Nov-14 0:36am    
+5
DamithSL 17-Nov-14 0:41am    
Thank you, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900