mkpriya,
I tried your code, and the first error I receive is:
" } expected"
the editor brings me to the last line of the code, so we might have a missing '}' to take care of first.
In any case, it looks to me that your string[] is declared outside any method. Hence, it is a class field (I guess public?). While you can declare an array of strings as a class field, and while you can initialize simple data fields, I don't think you can initialize array elements like that.
I believe you want to move the lines initializing the array elements in the class constructor (or maybe in a new static constructor?).
So you would have something like this:
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace String
{
public class Form1 : System.Windows.Forms.Form
{
private string[] path = new string[4];
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
path[0] = "c:\\digitalphotos\\Image1";
path[1] = "c:\\digitalphotos\\Image1";
path[2] = "c:\\digitalphotos\\Image2";
path[3] = "c:\\digitalphotos\\Image3";
}
//End Constructor
}
//End class
}
//End namespace
*** BY THE WAY: I'd suggest changing the name of the namespace to something less prone to trouble :-) ***
The code above defines the array as a private field of the object (and instanciates it too!). Then, the constructor fills in the elements.
In regard to your other wuestion, on how to create an array of PictureBoxes, I did something like that not long ago...
Basically you define an array of PictureBoxes as a class field, and then instanciate each element (and its characteristics) in the class constructor. You also need Bitmaps to stick in the boxes.
For instance:
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace Sample
{
public class Form1 : System.Windows.Forms.Form
{
private static int NUM_BOXES = 4;
private System.Windows.Forms.PictureBox[] pboxes =
new System.Windows.Forms.PictureBox[NUM_BOXES];
private string[] sources = new string[4];
private Bitmap[] bmaps = new Bitmap[NUM_BOXES];
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//Set the sources:
sources[0] = "c:\\digitalphotos\\Image1";
sources[1] = "c:\\digitalphotos\\Image1";
sources[2] = "c:\\digitalphotos\\Image2";
sources[3] = "c:\\digitalphotos\\Image3";
//Set the bitmaps:
for(int i=0; i<NUM_BOXES; i++)
bmap[i] = new Bitmap[sources[i];
//Set the picture boxes:
//We also add them to the form, one by one.
//(we could use the AddRange to do the same at the end)
this.SuspendLayout();
for(int i=0; i<NUM_BOXES; i++)
{
this.pboxes[i] = new System.Windows.Forms.PictureBox();
//Set the image displayed:
pboxes[i].Image = bmaps[i];
//Set any of the other attributes you want, e.g.:
this.pboxes[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pboxes[i].Location = new System.Drawing.Point(16, 16);
this.pboxes[i].Name = "Image_"+(i.ToString());
this.pboxes[i].Size = new System.Drawing.Size(72, 48);
this.pboxes[i].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pboxes[i].TabIndex = i;
this.pboxes[i].TabStop = false;
if(i==0) this.pboxes[i].Visible = true; //Set the first one to
else this.pboxes[i].Visible = false; //visible!
this.pboxes[i].Click += new System.EventHandler(this.picBox_Click);
this.Controls.Add(pboxes[i]);
}//FOR
this.ResumeLayout(false);
}
//End Constructor
}
//End class
}
//End namespace
I think that should do it, or get you closer :-)
Hope you'll forgive me if I don't test it right now...
Good Luck! And let me know if that doesn't help at all!