prit005 0 Light Poster

In this form I use Two button(1.Browse,2.Generate PictureBOx Dynamically), One Textbox(Here I enter Number of Picture Box to Display at run time),Context Menu,and a FolderBrowseDialog.

When I click on Browse Button it opens the filebrowsedialog box from this I select the Image folder. then I enter number in Text Box to display Picture Box at run time.
pbs.ContextMenuStrip = docmenu; this context menu strip show only last image not the list of image file ie it is not increment the docmenu.
also if I enter 2 value in picture box it event only handle for the first picture box

 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].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                if (i % 2 == 0)
                {
                    x = 20;
                    y += 220;
                }
                else
                {
                    x += 320;
                }
            }
            if (folderBrowserDialog1.SelectedPath == "")
            {
                MessageBox.Show("1st Click the Browse... Button");
                button1.Focus();
            }
            else
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderBrowserDialog1.SelectedPath);
                if (dir.Exists == true)
                    dir.Refresh();
                txtPathName.Text = dir.FullName;
                for (int i = 0; i < pbs.Length; i++)
                {
                    foreach (FileInfo file in dir.GetFiles("*.jpg"))
                    {
                                System.Windows.Forms.ContextMenuStrip docmenu = new System.Windows.Forms.ContextMenuStrip();
                        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();

                        docmenu.Items.AddRange(new System.Windows.Forms.ToolStripMenuItem[] { toolmenus });
                        // pictureBox8.ContextMenuStrip = docmenu;    // show context menu on rt click
                        pbs[i].ContextMenuStrip = docmenu;                            
                        pbs[i].Name="PictureBox"+(i+1);

                    }
                }
            }

        }
    }

=========================
private void pbs_Click(object sender, EventArgs e) // picture box click
    {
        try
        {
            int j = 0;
            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.pbs[j].ImageLocation = imgshow;
            }
        }
        catch (System.Exception)
        {
        }
    }
Be a part of the DaniWeb community

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