Add Number of Picture Box Dynamically with different Images

Dynamic Image To Dynamically added Picture Box

In this article we can add the multiple images to picture box at run time and also different image to different picture box as we want. From file browse dialog box we select the folder if that folder contains the images like jpeg file that all .jpeg file name add it to Context Menus and click on the image name the particular image is displayed to respective Picture Box.

 

namespace selectSubMenu

{

    partial class Form1

    {

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.IContainer components = null;

 

        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

 

        #region Windows Form Designer generated code

 

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.components = new System.ComponentModel.Container();

            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);

            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();

            this.txtPathName = new System.Windows.Forms.TextBox();

            this.btnGenPictBox = new System.Windows.Forms.Button();

            this.txtPictValue = new System.Windows.Forms.TextBox();

            this.lblValue = new System.Windows.Forms.Label();

            this.btnReset = new System.Windows.Forms.Button();

            this.lblPictName = new System.Windows.Forms.Label();

            this.SuspendLayout();

            //

            // contextMenuStrip1

            //

            this.contextMenuStrip1.Name = "contextMenuStrip1";

            this.contextMenuStrip1.Size = new System.Drawing.Size(61, 4);

            //

            // txtPathName

            //

            this.txtPathName.Location = new System.Drawing.Point(597, 6);

            this.txtPathName.Name = "txtPathName";

            this.txtPathName.Size = new System.Drawing.Size(166, 20);

            this.txtPathName.TabIndex = 12;

            //

            // btnGenPictBox

            //

            this.btnGenPictBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            this.btnGenPictBox.Location = new System.Drawing.Point(291, 6);

            this.btnGenPictBox.Name = "btnGenPictBox";

            this.btnGenPictBox.Size = new System.Drawing.Size(171, 23);

            this.btnGenPictBox.TabIndex = 13;

            this.btnGenPictBox.Text = "Generate Picture Box...";

            this.btnGenPictBox.UseVisualStyleBackColor = true;

            this.btnGenPictBox.Click += new System.EventHandler(this.btnGenPictBox_Click);

            //

            // txtPictValue

            //

            this.txtPictValue.Location = new System.Drawing.Point(158, 9);

            this.txtPictValue.Name = "txtPictValue";

            this.txtPictValue.Size = new System.Drawing.Size(127, 20);

            this.txtPictValue.TabIndex = 14;

            //

            // lblValue

            //

            this.lblValue.AutoSize = true;

            this.lblValue.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            this.lblValue.ForeColor = System.Drawing.Color.Tomato;

            this.lblValue.Location = new System.Drawing.Point(2, 8);

            this.lblValue.Name = "lblValue";

            this.lblValue.Size = new System.Drawing.Size(155, 26);

            this.lblValue.TabIndex = 15;

            this.lblValue.Text = "Enter Number to Generate\r\nPicture Box";

            //

            // btnReset

            //

            this.btnReset.Location = new System.Drawing.Point(480, 4);

            this.btnReset.Name = "btnReset";

            this.btnReset.Size = new System.Drawing.Size(75, 23);

            this.btnReset.TabIndex = 16;

            this.btnReset.Text = "Reset";

            this.btnReset.UseVisualStyleBackColor = true;

            this.btnReset.Click += new System.EventHandler(this.btnReset_Click);

            //

            // lblPictName

            //

            this.lblPictName.AutoSize = true;

            this.lblPictName.Location = new System.Drawing.Point(556, 9);

            this.lblPictName.Name = "lblPictName";

            this.lblPictName.Size = new System.Drawing.Size(0, 13);

            this.lblPictName.TabIndex = 17;

            //

            // Form1

            //

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.AutoScroll = true;

            this.ClientSize = new System.Drawing.Size(775, 416);

            this.Controls.Add(this.lblPictName);

            this.Controls.Add(this.btnReset);

            this.Controls.Add(this.lblValue);

            this.Controls.Add(this.txtPictValue);

            this.Controls.Add(this.btnGenPictBox);

            this.Controls.Add(this.txtPathName);

            this.Name = "Form1";

            this.Text = "Form1";

            this.Load += new System.EventHandler(this.Form1_Load);

            this.ResumeLayout(false);

            this.PerformLayout();

 

        }

 

        #endregion

 

        private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;

        private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;

        private System.Windows.Forms.TextBox txtPathName;

        private System.Windows.Forms.Button btnGenPictBox;

        private System.Windows.Forms.TextBox txtPictValue;

        private System.Windows.Forms.Label lblValue;

        private System.Windows.Forms.Button btnReset;

        private System.Windows.Forms.Label lblPictName;

    }

}

 

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

 

namespace selectSubMenu

{

    public partial class Form1 : Form

    {

        #region Form Constroctor

        public Form1()

        {

            InitializeComponent();

            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;

            this.AutoSize = true;

            this.Padding = new Padding(0, 0, 20, 20);

            this.StartPosition = FormStartPosition.CenterScreen;

        }

#endregion

        #region Form Load

        private void Form1_Load(object sender, EventArgs e)

        {

            btnReset.Visible = false;

            lblPictName.Visible = false;

            txtPictValue.Click += new EventHandler(txtPictValue_Click);

        }

        #endregion End Load

 

        #region pbs_click Event

        private void pbs_Click(object sender, EventArgs e) // picture box click

        {

            try

            {

                        PictureBox currentButton = new PictureBox();                       

                        currentButton = (PictureBox)sender;

                        lblPictName.Text = currentButton.Name;

                        //lblPictName.Visible = true;                                                               

            }

 

            catch (System.Exception)

            {

            }

        }

        #endregion End pbs+click

        PictureBox[] pbs;

        #region btnGenPictBox_Click

        private void btnGenPictBox_Click(object sender, EventArgs e)

        {

            int num = 0;

           

            if (int.TryParse(txtPictValue.Text, out num))

            {

                int i = 0;

                pbs = new PictureBox[num];

                for (i = 0; i < pbs.Length; i++)

                {

                    pbs[i] = new PictureBox();

                    pbs[i].Name = "Picturebox" + (i + 1);

                    pbs[i].Location = new Point(20, pbs[i].Bottom + (i * 300));

                    pbs[i].Size = new Size(700, 500);

                    //pbs[i].MouseClick += new MouseEventHandler(pbs_Click);

 

                    this.Controls.Add(pbs[i]);

                    pbs[i].Click += new EventHandler(pbs_Click); // calling the PictureBox Click event....

                    pbs[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

               // }

                    int m=0;

                    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;                        

                        {

                            foreach (FileInfo file in dir.GetFiles("*.jpg"))

                            {

                                System.Windows.Forms.ToolStripMenuItem toolmenus = new System.Windows.Forms.ToolStripMenuItem();

                                toolmenus.Text = file.ToString();

                                m++;

                                //contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripMenuItem[] { toolmenus });

                                docmenu.Items.AddRange(new System.Windows.Forms.ToolStripMenuItem[] { toolmenus });

                                pbs[i].Name = "PictureBox" + i;

                                //pbs[i].ContextMenuStrip = this.contextMenuStrip1;   

                               

                                pbs[i].ContextMenuStrip =docmenu;                              

 

                                pbs[i].Click  += new EventHandler(pbs_Click);                               

                                toolmenus.Click+=new EventHandler(toolmenus_Click);

                            }

                        }

                    }                       

                    else

                        if (result == DialogResult.Cancel)

                        {

                            MessageBox.Show("You Cancled The Browsing....");

                        }

                   

                }

            }

 

        }

        #endregion

        #region reset Button

        private void btnReset_Click(object sender, EventArgs e)

        {

            if (pbs.Length > 0)

            {

                for (int i = 0; i < pbs.Length; i++)

                {System.Windows.Forms.ContextMenuStrip docmenu = new System.Windows.Forms.ContextMenuStrip();

                    txtPathName.Text = "";

                    txtPictValue.Text = "";

                    docmenu.Dispose();

                    pbs[i].Dispose();                   

                }

            }

        }

        #endregion

 

        #region show the Reset Button click into textbox

        private void txtPictValue_Click(object sender, EventArgs e)

        {

            btnReset.Visible = true;

        }

        #endregion

 

        #region Handle the ContextMenu click events

        private void toolmenus_Click(object sender, EventArgs e)

        {

            if (pbs.Length > 0)

            {

                PictureBox pb = new PictureBox();

                pb.Click += new EventHandler(pbs_Click);

                pb.Name = lblPictName.Text;

                for (int i = 0; i < pbs.Length; i++)                   

                {

                    if (pbs[i].Name == pb.Name)

                    {

                        {

                            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))

                                  pbs[i].Image = Image.FromFile(imgshow);

                            }

                        }

                    }

                }

            }

        }

        #endregion

    }

}

Ebook Download
View all
Learn
View all