Dear All, the below is my code it not works properly. I want to display the picture boxes dynamically ie run time it works fine but in my application have 1 Button (btnGenPictBox) click on it adds the no. of picture box and also at the same time it display folderBrowserDialog to select Image folder while selecting the image folger the list of images(ie Image Names) will display into the context menu but image list will only display for One picture box not for Second. so please help me on this to show context menu for each picture box and also to display respective image into the particular Picture Box.

namespace selectSubMenu
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.AutoSize = true;
            this.Padding = new Padding(0, 0, 20, 20);
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
        private void pictureBox8_Click(object sender, EventArgs e) // picture box click
        {
            try
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderBrowserDialog1.SelectedPath);
                if (dir.Exists == true)
                {
                    dir.Refresh();
                    System.Windows.Forms.ToolStripMenuItem myItem = default(System.Windows.Forms.ToolStripMenuItem);
                    myItem = (System.Windows.Forms.ToolStripMenuItem)sender;
                    string imgshow = dir + "\\" + myItem.ToString();
                    txtPathName.Text = imgshow.ToString();
                    //if (System.IO.File.Exists(imgshow))
                    //    this.pictureBox8.ImageLocation = imgshow;
                }
            }
            catch (System.Exception)
            {
            }
        } 
        int j=0;
        int k = 1;
        
        private void pbs_Click(object sender, EventArgs e) // picture box click
        {
            try
            {                  
                        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderBrowserDialog1.SelectedPath);
                        if (dir.Exists == true)
                        {
                            dir.Refresh();
                            System.Windows.Forms.ToolStripMenuItem myItem = default(System.Windows.Forms.ToolStripMenuItem);
                            myItem = (System.Windows.Forms.ToolStripMenuItem)sender;

                            string imgshow = dir + "\\" + myItem.ToString();
                            txtPathName.Text = imgshow.ToString();
                            this.Text = myItem.Name;
                            if (System.IO.File.Exists(imgshow))
                                                           
                               this.pbs[j].ImageLocation = imgshow;
                            
                               
                        }

                    //}
                //}
            }

            catch (System.Exception)
            {
            }
        }
       
       
        PictureBox[] pbs;
        private void btnGenPictBox_Click(object sender, EventArgs e)
        {
            int num = 0;
            if (int.TryParse(txtPictValue.Text, out num))
            {
                pbs = new PictureBox[num];
                int x = 20, y = 20;
                for (int i = 0; i < pbs.Length; i++)
                {
                    pbs[i] = new PictureBox();
                    pbs[i].Name = "Picturebox" + (i + 1);
                    pbs[i].Location = new Point(x, y);
                    pbs[i].Size = new Size(300, 300);
                    this.Controls.Add(pbs[i]);
                    pbs[i].Click+=new EventHandler(pbs_Click);
                    pbs[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    if (i % 2 == 0)
                    {
                        x = 20;
                        y += 220;
                    }
                    else
                    {
                        x += 320;
                    }
                    System.Windows.Forms.ContextMenuStrip docmenu = new System.Windows.Forms.ContextMenuStrip();
                    System.Windows.Forms.DialogResult result = folderBrowserDialog1.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderBrowserDialog1.SelectedPath);
                        if (dir.Exists == true)
                            dir.Refresh();
                        txtPathName.Text = dir.FullName;
                        string foldpath = txtPathName.Text;

                        foreach (FileInfo file in dir.GetFiles("*.jpg"))
                        {
                            //for(j=0;j<txtPathName.co)
                            System.Windows.Forms.ToolStripMenuItem toolmenus = new System.Windows.Forms.ToolStripMenuItem();
                            //toolmenus.Click += new System.EventHandler(pictureBox8_Click); // call the Event
                            toolmenus.Click += new System.EventHandler(pbs_Click);
                            toolmenus.Text = file.ToString();
                            k++;
                            docmenu.Items.AddRange(new System.Windows.Forms.ToolStripMenuItem[] { toolmenus });
                            //pictureBox8.ContextMenuStrip = docmenu;    // show context menu on rt click
                            pbs[i].ContextMenuStrip = docmenu;

                        }
                    }
                    else
                        if (result == DialogResult.Cancel)
                        {
                            MessageBox.Show("You Cancled The Browsing....");
                        }

                }
            }

        }
         
    }
}

can Any Buddy reply it........?

Line 57 is where the problem is.
Here you are directly referencing only the first PictureBox with index of j=0.
You need to convert sender in to a picture box and use that instead.
Try this.

if (System.IO.File.Exists(imgshow) && sender is PictureBox)
                ((PictureBox)sender).ImageLocation = imgshow;

This adds a test to confirm that the sender is a PictureBox to prevent invalid cast errors.
Then cast sender to a PictureBox to set the new picture location.

Posted in error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.