Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C#
Article

PictureBox Array in Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
2.47/5 (19 votes)
1 Nov 20072 min read 78.6K   3.2K   15   10
Visual Studio .NET not support Array of controls, I try to create PictureBox array

Introduction

In VB6 we can create many Controls (Label, TextBox, Button, PictureBox, …etc.) as an Array, but Visual Studio .NET does not support this work. I tried to create an Array of PictureBox controls in this article. My next article will be about creating an Array of Labels and Buttons.

Background

My article has two forms. The name of one is (frmThumb) and the name of other is (frmView). With the first form you can load all the images in any folder as thumbnails by clicking the button (Load images).

Screenshot - Image1.jpg

Then when you click any image you can view this image with its own size from the form (frmView).

Screenshot - Image2.jpg

The form (frmThumb) has:

  • One panel control (BackPanel) to hold images
  • The progress control (MyProgress) which works while images are loading.
  • Two buttons: one (btnLoad) to load images, the other (btnExit) to exit the application.

The form (frmView) has:

  • One pictureBox control (imgDisplay) to view the image you clicked, where I let the property SizeMode = AutoSize
  • A label (lblImageName) to view the name and full path of the image
  • A button (btnClose) to close the form and return to the form (frmThumb).

Using the Code

The important code is the code to create the Array:

C#
//
// Function to add PictureBox controls, You can determine number of controls
//
private void AddControls(int cNumber) 
{ 
    // assign number of controls (cNumber): 
    imgArray = new System.Windows.Forms.PictureBox[cNumber]; 
    for (int i = 0; i < cNumber; i++) 
    { 
        // Initialize one variable 
        imgArray[i] = new System.Windows.Forms.PictureBox(); 
    } 
}

I create ClickImage event:

C#
//
// The function ClickImage:
// 
private void ClickImage(Object sender, System.EventArgs e) 
{ 
    // On Click: load (ImageToShow) with (Tag) of the image, 
    // Tag of image is its name and path 
    ImageToShow = ((System.Windows.Forms.PictureBox) sender).Tag.ToString; 
    // then view this image on the form (frmView) 
    Thumbnail.frmView f = new Thumbnail.frmView(); 
    f.ShowDialog(); 
}

//
// Event of the image click:
// 
imgArray[i].Click += new System.EventHandler(ClickImage);

You can see the functions:

  • ImagesInFolder() to load images from any folder
  • ShowFolderImages() to view images as thumbnails and how write the tag of the image like this:
C#
// 
// Write Tag = name and full path of image: 
// 
imgArray[i].Tag = imgName[i];

and how add images on the panel:

C#
//
// add images to Panel: 
// 
this.BackPanel.Controls.Add(imgArray[i]);

Points of Interest

I learn how to create Array of Controls as labels and Buttons, we can create same as this article.

Remarks

Source files of this article and the folder for the images is:

..\ImageArray\bin\Debug\Images.

Last Words

I hope this article helps you to create an array of any control. I shall write another article about creating an array of Labels and Buttons.

If you have any ideas or if you find any problems please tell me. Just remember make it simple English because my English language is poor!

Thanks for Code Project and for all.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionRefreshing the List Pin
Maggi13MK20-Mar-16 14:33
Maggi13MK20-Mar-16 14:33 
QuestionThanks for the Work. Here is a News. Pin
Rockford Sol1-Dec-14 9:05
Rockford Sol1-Dec-14 9:05 
GeneralMy vote of 1 Pin
voicevon19-Nov-14 2:05
voicevon19-Nov-14 2:05 
Questionhaving some issue with the image alignment Pin
Dhruv Shah00716-Jul-13 2:37
Dhruv Shah00716-Jul-13 2:37 
QuestionThanks Pin
quangvinhits14-Sep-11 0:50
quangvinhits14-Sep-11 0:50 
AnswerRe: Thanks Pin
Mostafa Kaisoun14-Sep-11 1:36
Mostafa Kaisoun14-Sep-11 1:36 
Generalthanks for the concept Pin
Xeshan Ahmed10-Jul-11 22:57
Xeshan Ahmed10-Jul-11 22:57 
GeneralRe: thanks for the concept Pin
Mostafa Kaisoun11-Jul-11 5:27
Mostafa Kaisoun11-Jul-11 5:27 
QuestionGrammar Pin
Ri Qen-Sin1-Nov-07 16:45
Ri Qen-Sin1-Nov-07 16:45 
AnswerRe: Grammar Pin
Ri Qen-Sin3-Nov-07 4:38
Ri Qen-Sin3-Nov-07 4:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.