In my win form application having 1 button ,folderbrowseDialog box, Picturebox inside the Panel.

On Button click open folderbrowsedialog for open the images folder i want to add this list images to my contextmenu at runtime i get that images path at runtime but now i am not able to get the click on that context menu to select the path the respective image is not display at picturebox in c#

I want help for this.......
Thanks

private void button1_Click(object sender, EventArgs e)
        {
            int iCtr = 0;
            ContextMenuStrip docmenu = new System.Windows.Forms.ContextMenuStrip();
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                //MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
                foreach (string imageFile in files)
                {
                    //string imageFile = i.ToString();
                    //if (Image.FromFile(imageFile) != null)
                    //{
                        ToolStripMenuItem openimg = new ToolStripMenuItem();
                        openimg.Text = imageFile.ToString();
                        iCtr++;
                        docmenu.Items.AddRange(new ToolStripMenuItem[] { openimg });
                        pictureBox8.ContextMenuStrip = docmenu;
                        
                    //}
                }
            }

 

        }

You are not adding an event handler for the click event of the menu items.
You need to do something like this.

private void button1_Click(object sender, EventArgs e)
        {
            int iCtr = 0;
            ContextMenuStrip docmenu = new System.Windows.Forms.ContextMenuStrip();
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                //MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
                foreach (string imageFile in files)
                {
                    ToolStripMenuItem openimg = new ToolStripMenuItem();
                    openimg.Text = imageFile; //<-- imageFile is already a string!
                    openimg.Click += menuitem_Click; //<-- attach to click event handler
                    iCtr++; //<---????
                    docmenu.Items.Add(openimg); //<-- use Add to add a single item
                }
                pictureBox8.ContextMenuStrip = docmenu;
            }
        }

        // handle image context menu item click events
        private void menuitem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            // edit if not a toolstrip menu
            if (item == null)
                return;
            // do something with item
            // e.g. Open file based on filename in Text property
            MessageBox.Show(string.Format("Selected Image File = \"{0}\"", item.Text), "Item Selected");
        }

thanks for reply

Now I want to add the number of picture boxes to windows form dynamically. for eg. when user put the input in text box such as user enter 4 then Four picture boxes are added to the form at run time.
also I don't know how handle the context menu event also at run time for the above picture boxes in win form............

If you intend to use the same ContextMenu for each picture box then delcare the menu variable at form level instead of inside the button click handler.

To add a picture box at run time you create a new PictureBox object and add it to the form's (or other container's) Controls collection after setting the Size,Location and other properties.

To get an idea of how this is done, add a PictureBox to your form, set some of the properties and save the form. Then look in the <formname>.designer.cs file for how the Form Designer codes this for you.

If you get stuck, just post your efforts and someone will help you.

Thanks for reply

Your welcome.
If your question is answered then please mark the thread as solved so others know you no longer need help. Thanks.

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.