Hi guys, is this possible?

I want to click on a picture box and test the response with a message box. Only problem is, these picture box controls only get created on runtime.

public Form1()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            //this.PnlViewFolders.Click += new EventHandler(pictureBox1_Click);
           // this.pictureBox1.Click += new EventHandler(pictureBox1_Click); 
        }// picturebox controls are created dynamically
        // how do you create a mouse click event on a control that has not been created yet?
        void pictureBox1_Click(object sender, EventArgs e)
        {
            pictureBox1.Name = "" + Guid.NewGuid();
            MessageBox.Show(pictureBox1.Name);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<PictureBox> Pictures = null;
            PictureBoxCreator(ref Pictures);
            int a = PnlViewFolders.Controls.Count;

            Control root = new Control();
            List<Control> accumulator = new List<Control>();

            foreach (Control ctrl in this.PnlViewFolders.Controls)
            {
                accumulator.Add(root);
            }
        }

        void GetListOfFolders(ref int NumberOfDirectories, ref List<string> ListOfFolders) 
        {// get a number and a list of folders in a directory
            string[] GetFolders = Directory.GetDirectories(@"C:\Share");
            ListOfFolders = new List<string>(GetFolders);
            NumberOfDirectories = ListOfFolders.Count;
        }

        void GetListOfPictures(ref List<string> ListOfPictures) 
        {
            string[] GetPictures = Directory.GetDirectories(@"C:\Share");
            ListOfPictures = new List<string>(GetPictures);
        }

        void PictureBoxCreator(ref List<PictureBox> Pictures)
        {// get the number of folders in a directory and create the same number of picture boxes
            int NumberOfDirectories = 0;
            List<string> ListOfFolders = null;
            GetListOfFolders(ref NumberOfDirectories, ref ListOfFolders);
            List<string> ListOfPictures = null;
            GetListOfPictures(ref ListOfPictures);
            List<Control> accumulator = new List<Control>();
            List<PictureBox> accumulator2 = new List<PictureBox>();

           for (int NumberofPictureBoxes = 0; NumberofPictureBoxes < NumberOfDirectories; NumberofPictureBoxes++)
           {
                PictureBox picture = new PictureBox
                {
                    Name = "" + Guid.NewGuid(),
                    Size = new Size(150, 150),
                    Location = new Point(NumberofPictureBoxes * 152, 1),
                    SizeMode = PictureBoxSizeMode.StretchImage,
                    Image = Image.FromFile(ListOfPictures[NumberofPictureBoxes].ToString() + "\\1.jpg")
                };

                string asd = "asd" + Guid.NewGuid();
                picture.Name = "" + Guid.NewGuid();
                PnlViewFolders.Controls.Add(picture);
                accumulator.Add(picture);
           }
        }

Thanks

try this,

Add the event handler when you create the control:

        for (int NumberofPictureBoxes = 0; NumberofPictureBoxes < NumberOfDirectories; NumberofPictureBoxes++)
        {
            PictureBox picture = new PictureBox
            {
                Name = "" + Guid.NewGuid(),
                Size = new Size(150, 150),
                Location = new Point(NumberofPictureBoxes * 152, 1),
                SizeMode = PictureBoxSizeMode.StretchImage,
                Image = Image.FromFile(ListOfPictures[NumberofPictureBoxes].ToString() + "\\1.jpg")

            };
            picture.Click += new EventHandler(pictureBox_Click);
            string asd = "asd" + Guid.NewGuid();
            picture.Name = "" + Guid.NewGuid();
            PnlViewFolders.Controls.Add(picture);
            accumulator.Add(picture);
        }

In the event handler cast sender as a picturebox to identify which one was clicked:

    void pictureBox_Click(object sender, EventArgs e)
    {
        PictureBox ClickedpictureBox = (PictureBox)sender;
        ClickedpictureBox.Name = "" + Guid.NewGuid();
        MessageBox.Show(ClickedpictureBox.Name);
    }
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.