![]() |
Languages »
C# »
General
Intermediate
PictureBox Array in Visual Studio .NETBy Mostafa KaisounVisual Studio .NET not support Array of controls, I try to create PictureBox array |
C#1.0, Windows, .NET1.1VS.NET2003, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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).
Then when you click any image you can view this image with its own size from the form (frmView).
The form (frmThumb) has:
BackPanel) to hold images MyProgress) which works while images are loading. btnLoad) to load images, the other (btnExit) to exit the application. The form (frmView) has:
imgDisplay) to view the image you clicked, where I let the property SizeMode = AutoSize lblImageName) to view the name and full path of the image btnClose) to close the form and return to the form (frmThumb). The important code is the code to create the Array:
//
// 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:
//
// 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: //
// Write Tag = name and full path of image:
//
imgArray[i].Tag = imgName[i];
and how add images on the panel:
//
// add images to Panel:
//
this.BackPanel.Controls.Add(imgArray[i]);
I learn how to create Array of Controls as labels and Buttons, we can create same as this article.
Source files of this article and the folder for the images is:
..\ImageArray\bin\Debug\Images.
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.
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 1 Nov 2007 Editor: |
Copyright 2007 by Mostafa Kaisoun Everything else Copyright © CodeProject, 1999-2010 Web17 | Advertise on the Code Project |